Advertisement
Guest User

Untitled

a guest
Jun 8th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. package jdbcdemo;
  2.  
  3. import java.sql.*;
  4.  
  5. public class Driver {
  6.  
  7.     public static void main(String[] args) throws SQLException {
  8.  
  9.         Connection myConn = null;
  10.         Statement myStmt = null;
  11.         ResultSet myRs = null;
  12.        
  13.         try {
  14.             // 1. Get a connection to database
  15.             myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "root" , "admin");
  16.             System.out.println("1");
  17.             // 2. Create a statement
  18.             myStmt = myConn.createStatement();
  19.             System.out.println("12");
  20.  
  21.             // 3. Execute SQL query
  22.             myRs = myStmt.executeQuery("select * from employees");
  23.             System.out.println("123");
  24.  
  25.             // 4. Process the result set
  26.             while (myRs.next()) {
  27.                 System.out.println(myRs.getString("last_name") + ", " + myRs.getString("first_name"));
  28.             }
  29.         }
  30.         catch (Exception exc) {
  31.             exc.printStackTrace();
  32.             System.out.println("1234");
  33.  
  34.         }
  35.         finally {
  36.             if (myRs != null) {
  37.                 myRs.close();
  38.             }
  39.            
  40.             if (myStmt != null) {
  41.                 myStmt.close();
  42.             }
  43.            
  44.             if (myConn != null) {
  45.                 myConn.close();
  46.             }
  47.         }
  48.     }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement