Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Properties;
- public class MySQL
- {
- private Connection con;
- private Statement st;
- private ResultSet rs;
- private PreparedStatement ps;
- private Main r;
- public MySQL(Main reg)
- {
- this.r = reg;
- }
- protected boolean loadDriver()
- {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- return true;
- }
- catch (ClassNotFoundException ex)
- {
- this.r.getLogger().severe("Could not load JDBC driver!");
- ex.printStackTrace();
- }
- return false;
- }
- protected String getJdbcUrl()
- {
- String host = this.r.getConfig().getString("connection.hostname");
- String port = this.r.getConfig().getString("connection.port");
- String name = this.r.getConfig().getString("connection.name");
- String url = "jdbc:mysql://" + host + ":" + port + "/" + name;
- return url;
- }
- protected Properties getProperties()
- {
- String user = this.r.getConfig().getString("connection.username");
- String pass = this.r.getConfig().getString("connection.password");
- Properties prop = new Properties();
- prop.put("autoReconnect", "true");
- prop.put("user", user);
- prop.put("password", pass);
- prop.put("useUnicode", "true");
- prop.put("characterEncoding", "utf8");
- return prop;
- }
- public boolean connect()
- {
- if (loadDriver())
- {
- this.r.getLogger().info("Connecting to database...");
- try
- {
- this.con = DriverManager.getConnection(getJdbcUrl(), getProperties());
- this.r.getLogger().info("Connected to database!");
- return true;
- }
- catch (SQLException ex)
- {
- this.r.getLogger().severe("Could not connect to database!");
- ex.printStackTrace();
- return false;
- }
- }
- return false;
- }
- public boolean close()
- {
- this.r.getLogger().info("Disconnecting from database...");
- if (isConn()) {
- try
- {
- this.con.close();
- this.r.getLogger().info("Disconnected from database!");
- return true;
- }
- catch (SQLException ex)
- {
- this.r.getLogger().severe("Could not disconnect from database!");
- return false;
- }
- }
- this.r.getLogger().severe("No connection detected!");
- return false;
- }
- public boolean isConn()
- {
- if (this.con != null) {
- try
- {
- if (this.con.isValid(1)) {
- return true;
- }
- return false;
- }
- catch (SQLException ex)
- {
- this.r.getLogger().severe("Database error!");
- ex.printStackTrace();
- return false;
- }
- }
- return false;
- }
- public ResultSet query(String query)
- throws SQLException
- {
- if (isConn())
- {
- this.st = this.con.createStatement();
- this.st.execute(query);
- this.rs = this.st.getResultSet();
- return this.rs;
- }
- this.r.getLogger().severe("Database error!");
- return null;
- }
- public ResultSet query(PreparedStatement ps)
- throws SQLException
- {
- if (isConn())
- {
- ps.execute();
- this.rs = ps.getResultSet();
- return this.rs;
- }
- this.r.getLogger().severe("Database error!");
- return null;
- }
- public PreparedStatement prepare(String query)
- throws SQLException
- {
- if (isConn())
- {
- this.ps = this.con.prepareStatement(query);
- return this.ps;
- }
- this.r.getLogger().severe("Database error!");
- return null;
- }
- public boolean isTable(String table)
- {
- try
- {
- String query = "SELECT * FROM " + table;
- this.st = this.con.createStatement();
- this.st.executeQuery(query);
- return true;
- }
- catch (SQLException ex) {}
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement