Advertisement
keymasterviriya1150

JDBC

Dec 3rd, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1.  
  2. /*
  3.  * To change this license header, choose License Headers in Project Properties.
  4.  * To change this template file, choose Tools | Templates
  5.  * and open the template in the editor.
  6.  */
  7. package jdbc;
  8.  
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14. import java.util.Scanner;
  15.  
  16. public class MyClass {
  17.  
  18.     public static void main(String[] args) throws SQLException {
  19.  
  20.         Connection connect = null;
  21.  
  22.         try {
  23.             Class.forName("com.mysql.jdbc.Driver");
  24.             // connect
  25.             connect = DriverManager.getConnection("jdbc:mysql://localhost/databasename"
  26.                     + "?user=root&password=");
  27.  
  28.             if (connect != null) {
  29.                 System.out.println("Database Connected.");
  30.             } else {
  31.                 System.out.println("Database Connect Failed.");
  32.             }
  33.  
  34.         } catch (Exception e) {
  35.             // TODO Auto-generated catch block
  36.             e.printStackTrace();
  37.         }
  38.  
  39.         Statement stmt = connect.createStatement();
  40.         ResultSet rs;
  41.         String sql="SELECT * FROM customer";
  42.         rs = stmt.executeQuery(sql);
  43.  
  44.          while ( rs.next() ) {
  45.                 String CustomerID = rs.getString("CustomerID");
  46.                 String Name = rs.getString("Name");
  47.                 System.out.println(CustomerID);
  48.                 System.out.println(Name);
  49.             }
  50.        
  51.         String x;
  52.         Scanner sc= new Scanner(System.in);
  53.        
  54.  
  55.         try {
  56.             if (connect != null) {
  57.                 connect.close();
  58.             }
  59.         } catch (SQLException e) {
  60.             // TODO Auto-generated catch block
  61.             e.printStackTrace();
  62.         }
  63.  
  64.     }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement