Advertisement
wingman007

JavaConnecting2DB

Nov 22nd, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package database_console;
  8.  
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.SQLException;
  12.  
  13. import java.sql.Statement;
  14. import java.sql.ResultSet;
  15.  
  16. /**
  17.  *
  18.  * @author Stoyan
  19.  */
  20. public class DBConnect {
  21.  
  22.     /**
  23.      * @param args the command line arguments
  24.      */
  25.     public static void main(String[] args) {
  26.         // TODO code application logic here
  27. //C:\Program Files\Java\jdk1.8.0_25\db\lib\derbyclient.jar
  28.         try {
  29.             String host = "jdbc:derby://localhost:1527/Employees";
  30.             String uName = "stoyanUserName";
  31.             String uPass= "password";
  32.             Connection con = DriverManager.getConnection( host, uName, uPass );
  33.            
  34.             Statement stmt = con.createStatement( );
  35.             String SQL = "SELECT * FROM Workers";
  36.             ResultSet rs = stmt.executeQuery( SQL );
  37.            
  38.             while(rs.next()) {
  39.                 int id_col = rs.getInt("ID");
  40.                 String first_name = rs.getString("First_Name");
  41.                 String last_name = rs.getString("Last_Name");
  42.                 String job = rs.getString("Job_Title");
  43.                 System.out.println( id_col + " " + first_name + " " + last_name + " " + job );
  44.             }
  45.             // Statement stmt = con.createStatement( RecordSet.TYPE_SCROLL_SENSITIVE );
  46.         }
  47.         catch ( SQLException err ) {
  48.             System.out.println( err.getMessage( ) );
  49.         }
  50.     }
  51.    
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement