Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.io.*;
- import java.util.*;
- public class mySQLEngine {
- private Connection conn;
- private String url, userName, password, catalogue;
- public mySQLEngine(String userName, String password){
- conn = null;
- this.userName = userName;
- this.password = password;
- }
- public void connnect() {
- InputStream is = getClass().getResourceAsStream("mySQLEngine.properties");
- Properties p = new Properties();
- try {
- p.load(is);
- url = p.getProperty("connectionURL");
- catalogue = p.getProperty("catalogue");
- }
- catch (IOException e) {
- System.err.println("error loading properties...");
- }
- try
- {
- Class.forName("com.mysql.jdbc.Driver").newInstance ();
- conn = DriverManager.getConnection(url+catalogue, userName, password);
- System.out.println ("Database connection established");
- }
- catch (Exception e)
- {
- System.err.println ("Cannot connect to database server");
- }
- }
- public void closeConnection(){
- try {
- conn.close();
- } catch (SQLException e) {
- System.err.println("SQLException: " + e.getMessage());
- }
- }
- public ResultSet executeQuery(String sqlStatement) throws SQLException{
- ResultSet rs = null;
- try{
- Statement stmt = conn.createStatement();
- if(stmt.execute(sqlStatement)){
- rs = stmt.getResultSet();
- }
- }
- catch (SQLException e) {
- throw e;
- }
- return rs;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement