Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.hansehans.divewars.mysql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class Database
- {
- private String database;
- private String host;
- private String port;
- private String username;
- private String password;
- private Connection con;
- public Database(String host, String port, String username, String password, String name)
- {
- this.host = host;
- this.port = port;
- this.username = username;
- this.password = password;
- this.database = name;
- }
- public void connect()
- {
- if (this.con == null) {
- try
- {
- this.con = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?autoReconnect=true", this.username, this.password);
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- }
- public void disconnect()
- {
- if (this.con != null) {
- try
- {
- this.con.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- }
- public void update(String query)
- {
- try
- {
- Statement st = this.con.createStatement();
- st.executeUpdate(query);
- st.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- public ResultSet updateWithResult(String query)
- {
- ResultSet rs = null;
- try
- {
- Statement st = this.con.createStatement();
- rs = st.executeQuery(query);
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return rs;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement