Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static String host;
- public static String user;
- public static String pass;
- public static int port;
- public static String db;
- private Connection conn;
- public MySQLHandler(){
- }
- public void closeConnection()
- {
- try
- {
- if (this.conn != null)
- {
- this.conn.close();
- }
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- finally
- {
- this.conn = null;
- }
- }
- public void closeRessources(ResultSet rs, PreparedStatement st)
- {
- if (rs != null)
- {
- try
- {
- rs.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- if (st != null)
- {
- try
- {
- st.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- }
- public Connection getConnection()
- {
- return this.conn;
- }
- public boolean hasConnection()
- {
- try
- {
- return (this.conn != null) || (this.conn.isValid(1));
- }
- catch (SQLException e)
- {
- }
- return false;
- }
- public Connection openConnection()
- throws Exception
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection("jdbc:mysql://" + MySQLHandler.host + ":" + MySQLHandler.port + "/" + MySQLHandler.db, MySQLHandler.user, MySQLHandler.pass);
- this.conn = conn;
- return conn;
- }
- public void plain_query(String s)
- {
- try
- {
- Connection conn = openConnection();
- PreparedStatement st = null;
- try
- {
- st = conn.prepareStatement(s);
- st.executeUpdate();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- finally
- {
- closeRessources(null, st);
- closeConnection();
- }
- }
- catch (Exception e1)
- {
- e1.printStackTrace();
- }
- }
- public ResultSet query(String s)
- {
- try
- {
- Connection conn = openConnection();
- PreparedStatement st = null;
- ResultSet rs = null;
- try
- {
- st = conn.prepareStatement(s);
- rs = st.executeQuery();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return rs;
- }
- catch (Exception e1)
- {
- e1.printStackTrace();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement