简体   繁体   English

使用JDBC URL连接到数据库

[英]Connecting to database with JDBC URL

My requirement is to check can we connect to a database with no schema defined through a java program and create a database. 我的要求是检查是否可以通过Java程序连接到未定义任何模式的数据库并创建数据库。 this works fine 这很好

Connection  connection =
           DriverManager.getConnection("jdbc:db2://wcspocca-db.cloudapp.net:50001/db2inst1","vmadmin","password@123;");

database: db2 schema: db2inst1 数据库:db2模式:db2inst1

but

Connection  connection =
           DriverManager.getConnection("jdbc:db2://wcspocca-db.cloudapp.net:50001/","vmadmin","password@123;");

fails WHY? 为什么失败?

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
DriverManager.getConnection("jdbc:db2://wcspocca-db.cloudapp.net:50001/!!DBNAME!!","vmadmin","password@123;");

Because in the second example the database is not specified. 因为在第二个示例中未指定数据库。 You can find the requirements in the DB2 JDBC documentation : 您可以在DB2 JDBC文档中找到需求:

>>-+-jdbc:db2:------+--//--server--+---------+--/--database----->
   +-jdbc:db2j:net:-+              '-:--port-'
   '-jdbc:ids:------'

>--+---------------------------+-------------------------------\><
   '-:--| connection-options |-'

The database parameter is not optional. 数据库参数不是可选的。

How I usually connect to a database using jave is like the following: Spacing it out to allow you to read the text below 我通常如何使用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";

In the method where you want to check the driver and connection I usually do it like this 在您要检查驱动程序和连接的方法中,我通常这样做

//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());
    }

The first try catch code registers the driver to the SQL developer then the second try catch sets up the connection to the database. 第一个try catch代码将驱动程序注册到SQL开发人员,然后第二个try catch建立与数据库的连接。 This helped save me a lot of head aches and I find it the most Direct route when using java. 这帮助我节省了很多头痛,并且在使用Java时我发现它是最直接的路线。

check the url carefully, after the port number if you do not put database name which you want to connect then how it will connect. 仔细检查URL,在端口号之后,如果您未输入要连接的数据库名称,则它将如何连接。 Its like you are giving a home address whitout the plot number. 就像您要给家庭住址写上地块编号一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM