Advertisement
Guest User

Untitled

a guest
Aug 10th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. import java.sql.*;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class mySQLEngine {
  6.  
  7.     private Connection conn;
  8.     private String url, userName, password, catalogue;
  9.  
  10.     public mySQLEngine(String userName, String password){
  11.        
  12.         conn = null;
  13.         this.userName = userName;
  14.         this.password = password;
  15.     }
  16.  
  17.     public void connnect() {
  18.  
  19.         InputStream is = getClass().getResourceAsStream("mySQLEngine.properties");
  20.         Properties p = new Properties();
  21.        
  22.         try {
  23.             p.load(is);
  24.             url = p.getProperty("connectionURL");
  25.             catalogue = p.getProperty("catalogue");
  26.         }
  27.         catch (IOException e) {
  28.             System.err.println("error loading properties...");
  29.         }
  30.         try
  31.           {
  32.  
  33.             Class.forName("com.mysql.jdbc.Driver").newInstance ();
  34.  
  35.             conn = DriverManager.getConnection(url+catalogue, userName, password);
  36.             System.out.println ("Database connection established");
  37.           }
  38.           catch (Exception e)
  39.           {
  40.               System.err.println ("Cannot connect to database server");
  41.  
  42.           }
  43.     }
  44.  
  45.     public void closeConnection(){
  46.  
  47.         try {
  48.             conn.close();
  49.  
  50.         } catch (SQLException e) {
  51.             System.err.println("SQLException: " + e.getMessage());
  52.  
  53.         }
  54.     }
  55.  
  56.     public ResultSet executeQuery(String sqlStatement) throws SQLException{
  57.         ResultSet rs = null;
  58.  
  59.         try{
  60.             Statement stmt = conn.createStatement();
  61.  
  62.             if(stmt.execute(sqlStatement)){
  63.                 rs = stmt.getResultSet();
  64.                
  65.             }
  66.         }
  67.         catch (SQLException e) {
  68.             throw e;
  69.         }
  70.        
  71.         return rs;
  72.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement