JDBC 连接到主机但不在数据库中
JDBC connect to host but not in database
我正在创建一个应用程序,您可以在其中管理一个 mySql 数据库,首先我希望用户通过提供主机名、用户名和密码来连接到主机。这是我的代码:
try {
Class.forName(ServerConnect.JDBC_DRIVER);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Cannot register JDBC Driver...");
}
try {
conn = DriverManager.getConnection("jdbc:mysql://"
+ toServer.getHostname() + "/hrmanagement?" + "user=" + toServer.getUsername()
+ "&password=" + toServer.getPassword());
System.out.println("Connected to the server!");
} catch (SQLException e) {
System.out
.println("Not Connected to the server. Make sure username or password is correct!");
}
我的问题是,我如何在不指定数据库的情况下进行连接,以便我可以让我的用户选择他可以连接到哪个数据库。我尝试删除“/hrmanagement?”但它没有连接到服务器。我能做什么?
The JDBC URL format for MySQL Connector/J is as follows, with items in square brackets ([, ]) being optional:
jdbc:mysql://[host][,failoverhost...][:port]/[database] »
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...
所以,删除 hrmanagement
,注意 /
和 ?user...
部分保留
my question is, how can i connect without specifying the database so
that i can let my user choose which database can he connect to. i
tried removing "/hrmanagement?" but it didn't connect to the server.
what can i do?
将问号移至“?user=”。我认为另一个答案是正确的。
在更改数据库方面,我相信您可以使用 setCatalog() 来使用不同的数据库。
来自连接页面:
void setCatalog(String catalog)
throws SQLException
Sets the given catalog name in order to select a subspace of this
Connection object's database in which to work.
If the driver does not support catalogs, it will silently ignore this
request.
Calling setCatalog has no effect on previously created or prepared
Statement objects. It is implementation defined whether a DBMS prepare
operation takes place immediately when the Connection method
prepareStatement or prepareCall is invoked. For maximum portability,
setCatalog should be called before a Statement is created or prepared.
Parameters:
catalog - the name of a catalog (subspace in this Connection object's database) in which to work Throws:
SQLException - if a database access error occurs or this method is called on a closed connection See Also:
getCatalog()
使用这个版本的调用可能会更容易,不那么混乱
public static Connection getConnection(String url,
String user,
String password)
throws SQLException
所以你的代码看起来像
try {
conn = DriverManager.getConnection("jdbc:mysql://" + toServer.getHostname(),
toServer.getUsername(),
toServer.getPassword());
conn.setCatalog("someDifferentDB");
System.out.println("Connected to the server!");
} catch (SQLException e) {
System.out.println(e.toString());
}
作为旁注,getConnection 将尝试为您加载 JDBC 驱动程序,因此您实际上不需要整个 Class.forName try/catch 块。这是大多数现代 JDBC 驱动程序不需要的旧代码的痕迹。仍然有一些人需要它,但我相信 mysql 符合更新的风格。只需确保驱动程序在命令行中的类路径中
java -cp \path\to\mysql\driver\driver.jar myProgram
我正在创建一个应用程序,您可以在其中管理一个 mySql 数据库,首先我希望用户通过提供主机名、用户名和密码来连接到主机。这是我的代码:
try {
Class.forName(ServerConnect.JDBC_DRIVER);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Cannot register JDBC Driver...");
}
try {
conn = DriverManager.getConnection("jdbc:mysql://"
+ toServer.getHostname() + "/hrmanagement?" + "user=" + toServer.getUsername()
+ "&password=" + toServer.getPassword());
System.out.println("Connected to the server!");
} catch (SQLException e) {
System.out
.println("Not Connected to the server. Make sure username or password is correct!");
}
我的问题是,我如何在不指定数据库的情况下进行连接,以便我可以让我的用户选择他可以连接到哪个数据库。我尝试删除“/hrmanagement?”但它没有连接到服务器。我能做什么?
The JDBC URL format for MySQL Connector/J is as follows, with items in square brackets ([, ]) being optional:
jdbc:mysql://[host][,failoverhost...][:port]/[database] » [?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...
所以,删除 hrmanagement
,注意 /
和 ?user...
部分保留
my question is, how can i connect without specifying the database so that i can let my user choose which database can he connect to. i tried removing "/hrmanagement?" but it didn't connect to the server. what can i do?
将问号移至“?user=”。我认为另一个答案是正确的。
在更改数据库方面,我相信您可以使用 setCatalog() 来使用不同的数据库。
来自连接页面:
void setCatalog(String catalog) throws SQLException
Sets the given catalog name in order to select a subspace of this Connection object's database in which to work.
If the driver does not support catalogs, it will silently ignore this request.
Calling setCatalog has no effect on previously created or prepared Statement objects. It is implementation defined whether a DBMS prepare operation takes place immediately when the Connection method prepareStatement or prepareCall is invoked. For maximum portability, setCatalog should be called before a Statement is created or prepared.
Parameters: catalog - the name of a catalog (subspace in this Connection object's database) in which to work Throws: SQLException - if a database access error occurs or this method is called on a closed connection See Also: getCatalog()
使用这个版本的调用可能会更容易,不那么混乱
public static Connection getConnection(String url,
String user,
String password)
throws SQLException
所以你的代码看起来像
try {
conn = DriverManager.getConnection("jdbc:mysql://" + toServer.getHostname(),
toServer.getUsername(),
toServer.getPassword());
conn.setCatalog("someDifferentDB");
System.out.println("Connected to the server!");
} catch (SQLException e) {
System.out.println(e.toString());
}
作为旁注,getConnection 将尝试为您加载 JDBC 驱动程序,因此您实际上不需要整个 Class.forName try/catch 块。这是大多数现代 JDBC 驱动程序不需要的旧代码的痕迹。仍然有一些人需要它,但我相信 mysql 符合更新的风格。只需确保驱动程序在命令行中的类路径中
java -cp \path\to\mysql\driver\driver.jar myProgram