Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.camdenorrb.ranks.MySql;
- import java.io.PrintStream;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class MySql
- {
- private String host = "";
- private String database = "";
- private String user = "";
- private String password = "";
- private Connection con;
- public MySql(String host, String database, String user, String password)
- {
- this.host = host;
- this.database = database;
- this.user = user;
- this.password = password;
- connect();
- }
- public synchronized void connect()
- {
- try
- {
- this.con = DriverManager.getConnection("jdbc:mysql://" + this.host + ":3306/" + this.database + "?autoReconnect=true", this.user, this.password);
- System.out.println("[MySQL] The connection to MySQL is made!");
- }
- catch (SQLException e)
- {
- connect();
- }
- }
- public synchronized void update(String qry)
- {
- try
- {
- PreparedStatement st = this.con.prepareStatement(qry);
- st.execute();
- st.close();
- }
- catch (Exception e)
- {
- connect();
- }
- }
- public boolean hasConnection()
- {
- return this.con != null;
- }
- public synchronized ResultSet query(String qry)
- {
- ResultSet rs = null;
- try
- {
- PreparedStatement st = this.con.prepareStatement(qry);
- rs = st.executeQuery();
- }
- catch (SQLException e)
- {
- connect();
- }
- return rs;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement