制作数据库类

时间:2017-06-07 17:05:37

标签: java mysql

我有以下课程:

public class Database  {
    private Connection connection;
    private MyApp instance = MyApp.getInstance();
    private String database, host, username, password, port;
    private Config config = new Config();

    public Connection Database() {
        database = config.get("database.database");
        host = config.get("database.host");
        username = config.get("database.username");
        password = config.get("database.password");
        port = config.get("database.port");
        try {
            connection = openConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return connection;
    }

    private Connection openConnection() throws SQLException, ClassNotFoundException {
        if (connection != null && !connection.isClosed()) {

        }

        synchronized (instance) {
            if (connection != null && !connection.isClosed()) {

            }
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
        }
        return connection;
    }

}

我希望能够在这个类上返回连接,这样我就可以在其他类中执行connection = new Database()。然后是Statement statement = connection.createStatement();

这可能并且感觉这不是最好的方式,我不确定什么是创建数据库类的更好方法。我的尝试是尝试在构造函数中执行return connection;但是错误了。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

你可以尝试这样的事情:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Baza {

private static java.sql.Connection conect = null;
private static final String url = "jdbc:mysql://localhost/Application?autoReconnect=true&useSSL=false";
private static final String username = "root";
private static final String password = "";
private final String urlWithoutBase= "jdbc:mysql://localhost:3306/";

public Baza() {
    try {
        conect = DriverManager.getConnection(urlWithoutBase, username, password);
        createDataBase();
        conect.close();
        openConnection();
        createTablePeople();
        conect.close();


    } catch (SQLException ex) {
       ex.printStackTrace();
    }    
}

public Connection openConnection() {
    try {
        conect = DriverManager.getConnection(url, username, password);
        return conect;
    } catch (SQLException ex) {
        ex.printStackTrace();
    }
    return conect;
}

public void closeConnection() {
    try {
        conect.close();
    } catch (SQLException ex) {
        ex.printStackTrace();
    }
}

 private void createDataBase() throws SQLException {
    Statement statement = conect.createStatement();
    String query = "CREATE DATABASE IF NOT EXISTS Application;";
    statement.execute(query);
}
 private void createTablePeople() throws SQLException{
      Statement statement = conect.createStatement();
    String query = "CREATE TABLE IF NOT EXISTS Osobe("
            + "id INT NOT NULL AUTO_INCREMENT,"
            + "name VARCHAR(30) NOT NULL,"
            + "surname VARCHAR(30) NOT NULL,"
            + "date VARCHAR(30) NOT NULL,"               
            + "PRIMARY KEY(id));";
    statement.execute(query);
 }       

}