Java methods to connect to various databases
- 2020-04-01 03:43:21
- OfStack
This article illustrates how Java can connect to various databases. Share with you for your reference. The details are as follows:
//MySQL :
String Driver="com.mysql.jdbc.Driver"; //Driver
String URL="jdbc:mysql://localhost:3306/db_name"; // Of the connection URL,db_name Is the database name
String Username="username"; //User name
String Password="password"; //Password < br / >
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
//Microsoft SQL Server 2.0 driver (the 3-jar one):
String Driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; //Method to connect to SQL database
String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name"; //db_name Is the database name
String Username="username"; //User name
String Password="password"; //Password < br / >
Class.forName(Driver).newInstance(); //Load data to drive
Connection con=DriverManager.getConnection(URL,UserName,Password); //
//Microsoft SQL Server 3.0 drive (1 a jar the ): // Old purple bamboo is perfect
String Driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; //Method to connect to SQL database
String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name"; //db_name Is the database name
String Username="username"; //User name
String Password="password"; //Password < br / >
Class.forName(Driver).newInstance(); //Load data to drive
Connection con=DriverManager.getConnection(URL,UserName,Password); //
// Sysbase:
String Driver="com.sybase.jdbc.SybDriver"; //Driver
String URL="jdbc:Sysbase://localhost:5007/db_name"; //db_name Name for data
String Username="username"; //User name
String Password="password"; //Password < br / >
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
//Oracle (in thin mode) : < br / >
String Driver="oracle.jdbc.driver.OracleDriver"; //Method to connect to the database
String URL="jdbc:oracle:thin:@loaclhost:1521:orcl"; //Orcl is the SID of the database
String Username="username"; //User name
String Password="password"; //Password < br / >
Class.forName(Driver).newInstance(); //Load database driver
Connection con=DriverManager.getConnection(URL,Username,Password);
//PostgreSQL:
String Driver="org.postgresql.Driver"; //Method to connect to the database
String URL="jdbc:postgresql://localhost/db_name"; //db_name Name for data
String Username="username"; //User name
String Password="password"; //Password < br / >
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
// DB2 :
String Driver="com.ibm.db2.jdbc.app.DB2.Driver"; //Connect to the Provider instance
with the DB2 client
//String Driver="com.ibm.db2.jdbc.net.DB2.Driver"; // Connection does not have DB2 The client's Provider The instance
String URL="jdbc:db2://localhost:5000/db_name"; //db_name Name for data
String Username="username"; //User name
String Password="password"; //Password < br / >
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
//Informix:
String Driver="com.informix.jdbc.IfxDriver";
String URL="jdbc:Informix-//sqli://localhost:1533/db_name:INFORMIXSER=myserver"; //db_name Name for data
String Username="username"; //User name
String Password="password"; //Password < br / >
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
// JDBC-ODBC:
String Driver="sun.jdbc.odbc.JdbcOdbcDriver";
String URL="jdbc:odbc:dbsource"; //Dbsource is the name of the data source
String Username="username"; //User name
String Password="password"; //Password < br / >
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
I hope this article has been helpful to your Java programming.