Guest User

Untitled

a guest
Feb 9th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.62 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.Date;
  8.  
  9. class MySQLAccess {
  10.   private Connection connect = null;
  11.   private Statement statement = null;
  12.   private PreparedStatement preparedStatement = null;
  13.   private ResultSet resultSet = null;
  14.  
  15.   public void readDataBase() throws Exception {
  16.     try {
  17.       // This will load the MySQL driver, each DB has its own driver
  18.       Class.forName("com.mysql.jdbc.Driver");
  19.       // Setup the connection with the DB
  20.       connect = DriverManager
  21.           .getConnection("jdbc:mysql://localhost/student"
  22.               , "root", "password");
  23.  
  24.       // Statements allow to issue SQL queries to the database
  25.       statement = connect.createStatement();
  26.       // Result set get the result of the SQL query
  27.       resultSet = statement
  28.           .executeQuery("select * from student.demo");
  29.       writeResultSet(resultSet);
  30.  
  31.       // PreparedStatements can use variables and are more efficient
  32.       preparedStatement = connect
  33.           .prepareStatement("insert into  student.demo values (?, ?)");
  34.       // "myuser, webpage, datum, summery, COMMENTS from FEEDBACK.COMMENTS");
  35.       // Parameters start with 1
  36.       preparedStatement.setString(1, "Deepak");
  37.       preparedStatement.setString(2, "MT2012022");
  38.       preparedStatement.execute();
  39.  
  40.       preparedStatement = connect
  41.           .prepareStatement("SELECT name, ID FROM student.demo");
  42.       resultSet = preparedStatement.executeQuery();
  43.       writeResultSet(resultSet);
  44.  
  45.       // Remove again the insert comment
  46.      /* preparedStatement = connect
  47.       .prepareStatement("delete from feedback.COMMENTS where myuser= ? ; ");
  48.       preparedStatement.setString(1, "Test");
  49.       preparedStatement.executeUpdate(); */
  50.      
  51.       resultSet = statement
  52.       .executeQuery("select * from student.demo");
  53.       writeMetaData(resultSet);
  54.      
  55.     } catch (Exception e) {
  56.       throw e;
  57.     } finally {
  58.       close();
  59.     }
  60.  
  61.   }
  62.  
  63.   private void writeMetaData(ResultSet resultSet) throws SQLException {
  64.     //   Now get some metadata from the database
  65.     // Result set get the result of the SQL query
  66.    
  67.    // System.out.println("The columns in the table are: ");
  68.    
  69. //    System.out.println("Table: " + resultSet.getMetaData().getTableName(1));
  70. //    for  (int i = 1; i<= resultSet.getMetaData().getColumnCount(); i++){
  71. //      System.out.println("Column " +i  + " "+ resultSet.getMetaData().getColumnName(i));
  72. //    }
  73.   }
  74.  
  75.   private void writeResultSet(ResultSet resultSet) throws SQLException {
  76.     // ResultSet is initially before the first data set
  77.     while (resultSet.next()) {
  78.       // It is possible to get the columns via name
  79.       // also possible to get the columns via the column number
  80.       // which starts at 1
  81.       // e.g. resultSet.getSTring(2);
  82.       String user = resultSet.getString("name");
  83.       String website = resultSet.getString("ID");
  84.      
  85.       System.out.println("Name: " + user);
  86.       System.out.println("ID: " + website);
  87.     }
  88.   }
  89.  
  90.   // You need to close the resultSet
  91.   private void close() {
  92.     try {
  93.       if (resultSet != null) {
  94.         resultSet.close();
  95.       }
  96.  
  97.       if (statement != null) {
  98.         statement.close();
  99.       }
  100.  
  101.       if (connect != null) {
  102.         connect.close();
  103.       }
  104.     } catch (Exception e) {
  105.  
  106.     }
  107.   }
  108.  
  109. }
  110. public class mysqldemo {
  111.       public static void main(String[] args) throws Exception {
  112.         MySQLAccess dao = new MySQLAccess();
  113.         dao.readDataBase();
  114.       }
  115.  
  116.  
  117.     }
Add Comment
Please, Sign In to add comment