Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class DBConnection {
- Connection conn;
- ResultSet resultSet;
- public ResultSet getResults(){
- return resultSet;
- }
- public DBConnection(){ }
- public void executeQuery(String query) {
- try{
- Statement st = conn.createStatement();
- st.executeQuery(query);
- resultSet = st.getResultSet();
- } catch(SQLException e ) { System.err.println ("Error message: " + e.getMessage ()); }
- }
- public void executeUpdate(String query) {
- try{
- Statement st = conn.createStatement();
- st.executeUpdate(query);
- } catch(SQLException e ) { System.err.println ("Error message: " + e.getMessage ()); }
- }
- public boolean createConnection(){
- try
- {
- String userName = "root";
- String password = "secure";
- String url = "jdbc:mysql://localhost/test";
- Class.forName ("com.mysql.jdbc.Driver").newInstance ();
- conn = DriverManager.getConnection (url, userName, password);
- System.out.println ("Database connection established");
- return true;
- }
- catch (Exception e)
- {
- System.err.println ("Cannot connect to database server");
- return false;
- }
- }
- public boolean destroyConnection(){
- if (conn != null)
- {
- try
- {
- conn.close ();
- System.out.println ("Database connection terminated");
- return true;
- }
- catch (Exception e) { /* ignore close errors */ }
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment