Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dblink;
- import java.sql.DriverManager;
- import javax.swing.JOptionPane;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Connection;
- public class DBConnectie {
- private static DBConnectie database;
- private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- private static final String DATABASE_URL = "jdbc:mysql://mysqlha2.ugent.be/BINFtest069";
- private Connection connection;
- public static DBConnectie getInstance() {
- if (database == null) {
- database = new DBConnectie();
- }
- return database;
- }
- public DBConnectie() {
- try {
- Class.forName(JDBC_DRIVER);
- connection = DriverManager.getConnection(DATABASE_URL, "BINFtest069", "RlrBjF2Hcx");
- } catch (SQLException sqlException) {
- JOptionPane.showMessageDialog(null, sqlException.getMessage(), "Error while connection to the database", JOptionPane.ERROR_MESSAGE);
- System.exit(1);
- } catch (ClassNotFoundException classNotFound) {
- JOptionPane.showMessageDialog(null, classNotFound.getMessage(), "Could not load driver", JOptionPane.ERROR_MESSAGE);
- System.exit(1);
- }
- }
- public Connection getConnection() {
- return connection;
- }
- public void closeConnection() {
- try {
- connection.close();
- } catch (SQLException sqlException) {
- JOptionPane.showMessageDialog(null, sqlException.getMessage(), "Cannot disconnect database", JOptionPane.ERROR_MESSAGE);
- System.exit(0);
- }
- }
- public ResultSet selectQuery(String sql) {
- try {
- return connection.createStatement().executeQuery(sql);
- } catch (SQLException e) {
- return null;
- }
- }
- public int insertQuery(String sql) {
- try {
- return connection.createStatement().executeUpdate(sql);
- } catch (SQLException e) {
- JOptionPane.showMessageDialog(null, e.getMessage());
- return 0;
- }
- }
- }
Add Comment
Please, Sign In to add comment