Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. package my_store;
  2.  
  3. import java.sql.*;
  4.  
  5. public class MyStoreLauncher {
  6.     public static void main(String[] args) {
  7.  
  8.         Connection conn = null;
  9.         Statement stmt = null;
  10.         ResultSet rs = null;
  11.        
  12.         try {
  13.             conn = DriverManager.getConnection("jdbc:sqlserver://localhost:3306","root", "Rekbr9422997");
  14.             Class.forName ("com.mysql.jdbc.Driver").newInstance ();
  15.             String sqlQuery = "SELECT * FROM users";
  16.            
  17.             stmt = conn.createStatement();
  18.            
  19.             rs = stmt.executeQuery(sqlQuery);
  20.            
  21.             while (rs.next()) {
  22.                 int id = rs.getInt("id");
  23.                 String job = rs.getString("LOGIN");
  24.                
  25.                 System.out.println("" + id +", " +job);
  26.             }
  27.         } catch(SQLException se) {
  28.             System.out.println("SQLError: " + se.getMessage() + " code: " + se.getErrorCode());
  29.         } catch (Exception e) {
  30.             System.out.println(e.getMessage());
  31.             e.printStackTrace();
  32.         } finally {
  33.             try {
  34.                 rs.close();
  35.                 stmt.close();
  36.                 conn.close();
  37.             } catch(Exception e) {
  38.                 e.printStackTrace();
  39.             }
  40.         }
  41.        
  42.     }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement