Advertisement
Guest User

Untitled

a guest
Aug 21st, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. package com.mycompany.app;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.Statement;
  5. import java.sql.ResultSet;
  6. class App{
  7.     public static void main(String args[]){
  8.         System.out.println("hello");
  9.         try{
  10. //step1 load the driver class
  11.  
  12.             Class.forName("com.mysql.cj.jdbc.Driver");
  13.  
  14. //step2 create  the connection object
  15.             Connection con= DriverManager
  16.                     .getConnection("jdbc:mysql://localhost:3306/centos?useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true","user","password");
  17.  
  18. //step3 create the statement object
  19.             Statement stmt=con.createStatement();
  20.  
  21. //step4 execute query
  22.             ResultSet rs = stmt.executeQuery("show tables");
  23.             /**  Format of Table is given
  24.             CREATE TABLE Employees (
  25. id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  26. firstname VARCHAR(30) NOT NULL,
  27. lastname VARCHAR(30) NOT NULL,
  28.  
  29. ); */
  30.  
  31.             rs = stmt.executeQuery("select  firstname,lastname from Employees");
  32.  
  33.             System.out.print("It works");
  34.             while(rs.next())
  35.                 System.out.println(rs.getString("firstname")+"  "+rs.getString("lastname"));
  36.  
  37. //step5 close the connection object
  38.             con.close();
  39.  
  40.         }catch(Exception e){ System.out.println(e);
  41.         e.printStackTrace();
  42.         }
  43.  
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement