Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MySQL;
- import java.sql.*;
- public class MySQLConnect
- {
- public static String HOST = "";
- public static String DATABASE = "";
- public static String USER = "";
- public static String PASSWORD = "";
- private Connection con;
- public MySQLConnect(String host, String database, String user, String password)
- {
- HOST = host;
- DATABASE = database;
- USER = user;
- PASSWORD = password;
- connect();
- }
- public void connect()
- {
- try
- {
- this.con = DriverManager.getConnection("jdbc:mysql://" + HOST + ":3306/" + DATABASE + "?autoReconnect=true", USER, PASSWORD);
- //Erfolgreich
- }
- catch (SQLException e)
- {
- //Verbindung fehlgeschlagen
- }
- }
- public void close()
- {
- try
- {
- if (this.con != null) {
- this.con.close();
- }
- }
- catch (SQLException localSQLException) {}
- }
- public void update(String qry)
- {
- try
- {
- Statement st = this.con.createStatement();
- st.executeUpdate(qry);
- st.close();
- }
- catch (SQLException e)
- {
- connect();
- System.err.println(e);
- }
- }
- public ResultSet query(String qry)
- {
- ResultSet rs = null;
- try
- {
- Statement st = this.con.createStatement();
- rs = st.executeQuery(qry);
- }
- catch (SQLException e)
- {
- connect();
- System.err.println(e);
- }
- return rs;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement