简体   繁体   中英

Connecting Java application to a database on a remote server

I am developing a Java application that needs a database. I know how to connect my application to a database that is stored in my local hard disk, but I need help to connect to a remote database.

    String host="jdbc:derby://localhost:1527/Test";

    Connection con=null;

    String uname="admins";

    String pass="admins";


    try {
        con=DriverManager.getConnection(host, uname, pass);
    } catch (SQLException ex) {
    }

The above code allows me to connect to a local database. What changes should I apply to connect to a database that is stored in a remote server?

Syntax for Derby JDBC database connection URL for the network client driver:

jdbc:derby://server[:port]/databaseName[;attribute=value]*

By defaultthe port is 1527 if omitted. suppose the user abc with password xyz wants to connect to the remote database remotedb on the server dbserver, use the following URL:

jdbc:derby://dbserver/remotedb;user=abc;password=xyz

One more thing as Russell Uhl said in his comment first make sure the remote server accepts connection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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