JAVA MysqlDataSource中的JDBC

时间:2017-09-27 10:24:22

标签: java mysql jdbc

我正在使用JDBC并使用MySqlDataSource进行连接。如何在DataSource.setDataBaseName()中添加模式名称之前创建模式?连接是可以的,我可以执行更新等等,但只有我在MysqlWorkbench中手动创建模式/数据库

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class DBHandler {

private String user = "root";
private String serverName = "localhost";
private String dbName = "TimeEdit";
private Connection connection;
private MysqlDataSource datasource = new MysqlDataSource();

public DBHandler(String passw) throws SQLException {
    datasource.setPassword(passw);
    datasource.setUser(user);
    datasource.setServerName(serverName);
    datasource.setDatabaseName(dbName);
    //datasource.setURL("jdbc:mysql://<localhost>:<3306>/");

}

public Connection getConnection() throws SQLException {
    return datasource.getConnection();
}

0 个答案:

没有答案