Advertisement
Guest User

DBConnect

a guest
Nov 21st, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.94 KB | None | 0 0
  1. package raport;
  2.  
  3. import java.sql.*;
  4.  
  5. public class DBConnect {
  6.     private Connection con;
  7.     private Statement st;
  8.     private ResultSet rs;
  9.    
  10.    
  11.     //Connect to MySQL
  12.     public DBConnect () {
  13.         try {
  14.             Class.forName("com.mysql.jdbc.Driver");
  15.            
  16.             con = DriverManager.getConnection("jdbc:mysql://localhost:3307/acraport","root","usbw");
  17.             st = con.createStatement();
  18.            
  19.         }catch(Exception ex) {
  20.             System.out.println("Error: "+ex);
  21.         }
  22.        
  23.     }
  24.    
  25.    
  26.     //IO query
  27.     public void getData(String query) {
  28.         try {
  29.             rs = st.executeQuery(query);
  30.             System.out.println("Recors from Database");
  31.             while(rs.next()) {
  32.                 String test_id = rs.getString("Id");
  33.                 String test_login = rs.getString("Login");
  34.                 String test_password = rs.getString("Password");
  35.                 System.out.println("Id: "+test_id+" Login: "+test_login+" Password: "+test_password);
  36.             }
  37.            
  38.         }catch(Exception ex) {
  39.             System.out.println("Error: "+ex);
  40.         }
  41.        
  42.     }
  43.    
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement