Advertisement
Guest User

baziucha

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