Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.herocraftonline.rightlegred.heroesleaderboard.persistance;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import com.herocraftonline.rightlegred.heroesleaderboard.HeroesLeaderboard;
- public class MySQL {
- // Create a String to hold the Database location.
- Connection con = null;
- String url = "testdb.db";
- String user = "testuser";
- String password = "test623";
- private final HeroesLeaderboard plugin;
- public MySQL(HeroesLeaderboard plugin) {
- this.plugin = plugin;
- }
- /**
- * Grab a new Database Connection
- * @return
- */
- public Connection getConnection() {
- Connection connection = null;
- try {
- con = DriverManager.getConnection(url, user, password);
- Statement st = con.createStatement();
- ResultSet result = st.executeQuery("SELECT VERSION()");
- if (result.next()) {
- System.out.println(result.getString(1));
- }
- con.close();
- } catch (SQLException ex) {
- System.out.println(ex.getMessage());
- }
- return connection;
- }
- /**
- * This function performs Inserts & Updates.
- * @param sqlString
- */
- public void tryUpdate(String sqlString) {
- try {
- plugin.log(Level.INFO, sqlString);
- Connection conn = getConnection();
- Statement st = conn.createStatement();
- st.executeUpdate(sqlString);
- st.close();
- conn.close();
- } catch (Exception e) {
- plugin.log(Level.WARNING, "The following statement failed: " + sqlString);
- plugin.log(Level.WARNING, "Statement failed: " + e.toString());
- }
- }
- /**
- * This function performs Selects and returns a ResultSet.
- * @param sqlString
- * @return
- */
- public ResultSet trySelect(String sqlString) {
- try {
- plugin.log(Level.INFO, sqlString);
- Connection conn = getConnection();
- Statement st = conn.createStatement();
- return st.executeQuery(sqlString);
- } catch (Exception e) {
- plugin.log(Level.WARNING, "Statement failed: " + e.toString());
- }
- return null;
- }
- /**
- * Return the Row Count of the Query.
- * @param query
- * @return
- */
- public int rowCount(String query){
- int count = 0;
- try {
- ResultSet r = trySelect(query);
- while(r.next()) {
- count++;
- }
- r.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement