Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.xplore.city;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class MySQL {
- public static String host = "46.20.34.169";
- public static String port = "3306";
- public static String database = "city";
- public static String username = "TheXplore";
- public static String password = "Random21";
- public static Connection con;
- public static Connection connect()
- {
- if(!hasConnection())
- {
- try
- {
- con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
- System.out.println("MySQL: Connected!");
- return con;
- }
- catch (SQLException e)
- {
- System.out.println("MySQL: Cant connect!");
- e.printStackTrace();
- return null;
- }
- }
- return null;
- }
- public static Connection getConnection()
- {
- if(!hasConnection())
- {
- return connect();
- }
- else
- return con;
- }
- public static void disconnect()
- {
- if(hasConnection())
- {
- try
- {
- con.close();
- System.out.println("MySQL: Verbindung geschlossen.");
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- }
- public static boolean hasConnection()
- {
- try
- {
- if(!(con == null))
- {
- if(!con.isValid(10))
- {
- System.out.println("MySQL: Connection timeout");
- }
- return con.isValid(10);
- }
- else
- {
- return false;
- }
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- return false;
- }
- }
- public static void execute(String query)
- {
- try
- {
- Statement st = getConnection().createStatement();
- st.execute(query);
- st.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- public static ResultSet select(String query)
- {
- ResultSet rs = null;
- try
- {
- Statement st = getConnection().createStatement();
- rs = st.executeQuery(query);
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return rs;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement