Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. //The source of this sample code is
  2. //http://www.tutorialspoint.com/jdbc/jdbc-sample-code.htm
  3.  
  4. //STEP 1. Import required packages
  5. import java.sql.*;
  6. import java.util.Scanner;
  7.  
  8. public class FirstExample {
  9.     // JDBC driver name and database URL
  10.     static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
  11.     static final String DB_URL = "jdbc:mysql://localhost/school";
  12.  
  13.     //  Database credentials
  14.     static final String USER = "root";
  15.     static final String PASS = "password";
  16.  
  17.     public static void main(String[] args) {
  18.         Connection conn = null;
  19.         Statement stmt = null;
  20.         try{
  21.             //STEP 2: Register JDBC driver
  22.             Class.forName("com.mysql.jdbc.Driver");
  23.  
  24.             //STEP 3: Open a connection
  25.             System.out.println("Connecting to database...");
  26.             conn = DriverManager.getConnection(DB_URL,USER,PASS);
  27.  
  28.             //STEP 4: Execute a query
  29.             System.out.println("Creating statement...");
  30.             stmt = conn.createStatement();
  31.  
  32.             String sql;
  33.             sql = "CREATE TABLE Students(firstName VARCHAR(20),lastName VARCHAR(20), ssn VARCHAR(9),major VARCHAR(20))";
  34.             stmt.executeUpdate(sql);
  35.  
  36.             Scanner in = new Scanner(System.in);
  37.  
  38.             for(int i=0;i<4;i++)
  39.             {
  40.                 String first="";
  41.                 String last="";
  42.                 String ssn= "";
  43.                 String major="";
  44.  
  45.                 System.out.println("Enter a first name");
  46.                 first = in.next();
  47.                 System.out.println("Enter a last name");
  48.                 last = in.next();
  49.                 System.out.println("Enter the 9 digit SSN");
  50.                 ssn = in.next().substring(0, 9);
  51.                 System.out.println("Enter a major");
  52.                 major = in.next();
  53.  
  54.                 sql = "INSERT INTO Students VALUES('"+first+"','"+last+"','"+ssn+"','"+major+"');";
  55.  
  56.                 stmt.executeUpdate(sql);
  57.             }
  58.  
  59.             sql = "SELECT * FROM Students";
  60.  
  61.             ResultSet rs = stmt.executeQuery(sql);
  62.  
  63.             //STEP 5: Extract data from result set
  64.             while(rs.next()){
  65.                 //Retrieve by column name
  66.                 String first    = rs.getString("firstName");
  67.                 String last     = rs.getString("lastName");
  68.                 String ssn      = rs.getString("ssn");
  69.                 String major    = rs.getString("major");
  70.                 //Display values
  71.                 System.out.println("Student:"+first+" "+last+ " :"+ssn+" : "+major);
  72.             }
  73.             //STEP 6: Clean-up environment
  74.             rs.close();
  75.             stmt.close();
  76.             conn.close();
  77.         }catch(SQLException se){
  78.             //Handle errors for JDBC
  79.             se.printStackTrace();
  80.         }catch(Exception e){
  81.             //Handle errors for Class.forName
  82.             e.printStackTrace();
  83.         }finally{
  84.             //finally block used to close resources
  85.             try{
  86.                 if(stmt!=null)
  87.                     stmt.close();
  88.             }catch(SQLException se2){
  89.             }// nothing we can do
  90.             try{
  91.                 if(conn!=null)
  92.                     conn.close();
  93.             }catch(SQLException se){
  94.                 se.printStackTrace();
  95.             }//end finally try
  96.         }//end try
  97.         System.out.println("Goodbye!");
  98.     }//end main
  99. }//end FirstExample
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement