Home>

In this article, we will mainly introduce the methods for connecting Java with Oracle, DB2, SQL Server, Sybase, MySQL, and Postgresql.

Oracle database

class.forname ("oracle.jdbc.driver.oracledriver"). newinstance ();
string url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl is the sid of the database
string user="test";
string password="test";
connection conn=drivermanager.getconnection (url, user, password);

2, db2 database

class.forname ("com.ibm.db2.jdbc.app.db2driver") .newinstance ();
string url="jdbc:db2://localhost:5000/sample";
//sample is your database name
string user="admin";
string password="";
connection conn=drivermanager.getconnection (url, user, password);

3, sql server database

class.forname ("com.microsoft.jdbc.sqlserver.sqlserverdriver"). newinstance ();
string url="jdbc:microsoft:sqlserver://localhost:1433;databasename=mydb";
//mydb is the database
string user="sa";
string password="";
connection conn=drivermanager.getconnection (url, user, password);

4, sybase database

class.forname ("com.sybase.jdbc.sybdriver"). newinstance ();
string url="jdbc:sybase:tds:localhost:5007/mydb";
//mydb is your database name
properties sysprops=system.getproperties ();
sysprops.put ("user", "userid");
sysprops.put ("password", "user_password");
connection conn=drivermanager.getconnection (url, sysprops);

5, mysql database

class.forname ("org.gjt.mm.mysql.driver"). newinstance ();
string url="jdbc:mysql://localhost/mydb"
//mydb is the database name
connection conn=drivermanager.getconnection (url);

6, postgresql database

class.forname ("org.postgresql.driver"). newinstance ();
string url="jdbc:postgresql://localhost/mydb"
//mydb is the database name
string user="myuser";
string password="mypassword";
connection conn=drivermanager.getconnection (url, user, password);
  • Previous Summary of PHP code to determine whether a visit is a search engine spider or an ordinary user
  • Next JQuery ajax processing method when returning JSON (three ways)