Guest User

Untitled

a guest
Jun 9th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 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. package auths;
  7.  
  8. import java.sql.*;
  9.  
  10.  
  11. /**
  12.  *
  13.  * @author adas
  14.  */
  15. public class AuthS {
  16.  
  17.     /**
  18.      * @param args the command line arguments
  19.      */
  20.     static final String DB_URL = "jdbc:mysql://someplace.ipagemysql.com/db_name";
  21.     static final String USER = "db_user";
  22.     static final String PASS = "db_pass";
  23.    
  24.     public static void main(String[] args) throws Exception {
  25.         // TODO code application logic here
  26.         AuthS as = new AuthS();
  27.         as.connectToDB();
  28.     }
  29.    
  30.     public void connectToDB() throws Exception{
  31.         Connection conn = null;
  32.         Statement stmt = null;
  33.         try {
  34.             Class.forName("com.mysql.jdbc.Driver");
  35.             System.out.println("Connecting to database...");
  36.             conn = DriverManager.getConnection(DB_URL,USER,PASS);
  37.             System.out.println("Creating statement...");
  38.             stmt = conn.createStatement();
  39.             String sql;
  40.             sql = "SELECT * from users";
  41.             ResultSet rs = stmt.executeQuery(sql);
  42.             while(rs.next()){
  43.                 String mUsername = rs.getString("username");
  44.                 String mHash = rs.getString("hash");
  45.  
  46.                 System.out.print(", First: " + mUsername);
  47.                 System.out.println(", Last: " + mHash);
  48.             }
  49.             rs.close();
  50.             stmt.close();
  51.             conn.close();
  52.            
  53.         } catch (Exception ex) {
  54.            
  55.         }
  56.        
  57.     }
  58.    
  59. }
Add Comment
Please, Sign In to add comment