Advertisement
flixbeat

JDBC sample

Aug 21st, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. package jdbctest;
  2.  
  3. //STEP 1. Import required packages
  4. import java.sql.*;
  5.  
  6. public class JdbcTest {
  7.    
  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/db_lighthouse";
  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 a selected database...");
  25.             conn = DriverManager.getConnection(DB_URL, USER, PASS);
  26.             System.out.println("Connected database successfully...");
  27.  
  28.             //STEP 4: Execute a query
  29.             System.out.println("Creating statement...");
  30.             stmt = conn.createStatement();
  31.  
  32.             String sql = "SELECT * FROM tbl_users";
  33.             ResultSet rs = stmt.executeQuery(sql);
  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 uname = rs.getString("uname");
  39.                 String fname = rs.getString("fname");
  40.                 String lname = rs.getString("lname");
  41.  
  42.                 //Display values
  43.                 System.out.print("ID: " + id);
  44.                 System.out.print(", Username: " + uname);
  45.                 System.out.print(", First: " + fname);
  46.                 System.out.println(", Last: " + lname);
  47.             }
  48.             rs.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.                     conn.close();
  60.                 }
  61.             } catch (SQLException se) {
  62.             }// do nothing
  63.             try {
  64.                 if (conn != null) {
  65.                     conn.close();
  66.                 }
  67.             } catch (SQLException se) {
  68.                 se.printStackTrace();
  69.             }//end finally try
  70.         }//end try
  71.         System.out.println("Goodbye!");
  72.     }
  73.    
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement