Advertisement
b1ack_c0de

FirstExample.java

Feb 19th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.94 KB | None | 0 0
  1. package personal;
  2.  
  3. //STEP 1. Import required packages
  4. import java.sql.*;
  5.  
  6. public class FirstExample {
  7.     // JDBC driver name and database URL
  8.     static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
  9.     static final String DB_URL = "jdbc:mysql://localhost/habib";
  10.  
  11.     //  Database credentials
  12.     static final String USER = "root";
  13.     static final String PASS = "habib";
  14.  
  15.     public static void main(String[] args) {
  16.         Connection conn = null;
  17.         Statement stmt = null;
  18.         try{
  19.             //STEP 2: Register JDBC driver
  20.             Class.forName("com.mysql.jdbc.Driver");
  21.  
  22.             //STEP 3: Open a connection
  23.             System.out.println("Connecting to database...");
  24.             conn = DriverManager.getConnection(DB_URL,USER,PASS);
  25.  
  26.             //STEP 4: Execute a query
  27.             System.out.println("Creating statement...");
  28.             stmt = conn.createStatement();
  29.             String sql;
  30.             sql = "SELECT id, name FROM info";
  31.             ResultSet rs = stmt.executeQuery(sql);
  32.  
  33.             //STEP 5: Extract data from result set
  34.             while(rs.next()){
  35.                 //Retrieve by column name
  36.                 int id  = rs.getInt("id");
  37.                 String name = rs.getString("name");
  38.  
  39.                 //Display values
  40.                 System.out.println("+--------------------------------------+");
  41.                 System.out.print("| ID: " + id);
  42.                 System.out.println("   | Name: " + name + " |");
  43.                 System.out.println("+--------------------------------------+");
  44.             }
  45.             //STEP 6: Clean-up environment
  46.             rs.close();
  47.             stmt.close();
  48.             conn.close();
  49.         }catch(SQLException se){
  50.             //Handle errors for JDBC
  51.             se.printStackTrace();
  52.         }catch(Exception e){
  53.             //Handle errors for Class.forName
  54.             e.printStackTrace();
  55.         }finally{
  56.             //finally block used to close resources
  57.             try{
  58.                 if(stmt!=null)
  59.                     stmt.close();
  60.             }catch(SQLException se2){
  61.             }// nothing we can do
  62.             try{
  63.                 if(conn!=null)
  64.                     conn.close();
  65.             }catch(SQLException se){
  66.                 se.printStackTrace();
  67.             }//end finally try
  68.         }//end try
  69.         System.out.println("Goodbye!");
  70.     }//end main
  71. }//end FirstExample
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement