|
To get a connection to a PostgreSQL database do this:
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://host/database";
Connection conn = DriverManager.getConnection(url,"username", "password");
Of course you must specify your own values for the host, database, username, and password.
Note that the PostgreSQL URL can be of these forms:
jdbc:postgresql:database
jdbc:postgresql://host/database
jdbc:postgresql://host:port/database
The phrase host refers to the computer you are trying to connect to, i.e., the computer that has PostgreSQL installed. It can be localhost for your computer, or a valid name or TCP/IP address for a remote computer system.
Cool. How do I connect to a MySQL database?
You connect the same way, but the Driver and URL are different. The code below shows the name of the Driver and the format of the URL.
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://host/database";
|