Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package rocky.myhub.SQL;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class MySQL {
- public static String host = "127.0.0.1";
- public static String port = "3306";
- public static String database = "minecraft";
- public static String username = "root";
- public static String password = "";
- public static Connection con;
- public static boolean isConnected(){
- return con != null;
- }
- public static void connect(){
- if(!isConnected()){
- try {
- con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static void disconnect(){
- try {
- con.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static PreparedStatement getStatement(String sql){
- if(isConnected()){
- PreparedStatement ps;
- try {
- ps = con.prepareStatement(sql);
- return ps;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- public static ResultSet getResult(String sql){
- if(isConnected()){
- PreparedStatement ps;
- ResultSet rs;
- try {
- ps = getStatement(sql);
- rs = ps.executeQuery();
- return rs;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement