我的要求是检查我们是否可以通过java程序连接到没有定义模式的数据库并创建一个数据库。这个很好用
Connection connection =
DriverManager.getConnection("jdbc:db2://wcspocca-db.cloudapp.net:50001/db2inst1","vmadmin","password@123;");数据库: db2模式: db2inst1
但
Connection connection =
DriverManager.getConnection("jdbc:db2://wcspocca-db.cloudapp.net:50001/","vmadmin","password@123;");为什么失败?
com.ibm.db2.jcc.am.mo: [jcc][10165][10045][4.7.85] Invalid database URL syntax: jdbc:db2://wcspocca-db.cloudapp.net:50001/. ERRORCODE=-4461, SQLSTATE=42815发布于 2015-06-01 20:48:32
DriverManager.getConnection("jdbc:db2://wcspocca-db.cloudapp.net:50001/!!DBNAME!!","vmadmin","password@123;");发布于 2015-06-01 20:39:06
因为在第二个示例中没有指定数据库。您可以在DB2 JDBC documentation中找到需求
>>-+-jdbc:db2:------+--//--server--+---------+--/--database----->
+-jdbc:db2j:net:-+ '-:--port-'
'-jdbc:ids:------'
>--+---------------------------+-------------------------------\><
'-:--| connection-options |-'数据库参数不是可选的。
发布于 2015-06-01 20:44:50
我通常使用jave连接到数据库的方式如下所示:将其隔开以允许您阅读以下文本
//Wide Scope Variables
static Connection conn = null; //right now this connection is off
static String dbURL = "database url"; //I use SQL developer and your able to grab the connection url there
static String user = "This can be empty and ask user for input of hard code the user name";
static String pass = "same as above";在你想要检查驱动程序和连接的方法中,我通常这样做
//Register the Driver
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
//Set up connection
try
{
conn = DriverManager.getConnection(dbURL,user,password);
conn.clearWarnings();
stmt = conn.createStatement();
}
catch(SQLException e)
{
System.err.println(e.getMessage());
}第一个SQL 代码向开发人员注册驱动程序,然后第二个try catch建立到数据库的连接。这帮我省去了很多头痛,而且我发现这是使用java时最直接的方法。
https://stackoverflow.com/questions/30573442
复制相似问题