This article describes the method of connecting Java to various databases. Share it for your reference. The details are as follows:
Copy the code as follows://MySQL:
String Driver="com.mysql.jdbc.Driver"; //Driver
String URL="jdbc:mysql://localhost:3306/db_name"; //The URL of the connection, db_name is the database name
String Username="username"; //Username
String Password="password"; //Password
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
//Microsoft SQL Server 2.0 driver (the one with 3 jars):
String Driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; //Methods to connect to SQL database
String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name"; //db_name is the database name
String Username="username"; //Username
String Password="password"; //Password
Class.forName(Driver).newInstance(); //Loading data can be driven
Connection con=DriverManager.getConnection(URL,UserName,Password); //
//Microsoft SQL Server 3.0 driver (one jar): // Lao Zizhu perfect
String Driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; //Methods to connect to SQL database
String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name"; //db_name is the database name
String Username="username"; //Username
String Password="password"; //Password
Class.forName(Driver).newInstance(); //Loading data can be driven
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 is a data nameable
String Username="username"; //Username
String Password="password"; //Password
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
//Oracle (using thin mode):
String Driver="oracle.jdbc.driver.OracleDriver"; //Methods to connect to the database
String URL="jdbc:oracle:thin:@loaclhost:1521:orcl"; //orcl is the SID of the database
String Username="username"; //Username
String Password="password"; //Password
Class.forName(Driver).newInstance(); //Load the database driver
Connection con=DriverManager.getConnection(URL,Username,Password);
//PostgreSQL:
String Driver="org.postgresql.Driver"; //Methods to connect to the database
String URL="jdbc:postgresql://localhost/db_name"; //db_name is a data nameable
String Username="username"; //Username
String Password="password"; //Password
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
// DB2:
String Driver="com.ibm.db2.jdbc.app.DB2.Driver"; //Connect Provider instance with DB2 client
//String Driver="com.ibm.db2.jdbc.net.DB2.Driver"; //Connect Provider instances that do not have DB2 client
String URL="jdbc:db2://localhost:5000/db_name"; //db_name is a data nameable
String Username="username"; //Username
String Password="password"; //Password
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 is a data nameable
String Username="username"; //Username
String Password="password"; //Password
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 data source name
String Username="username"; //Username
String Password="password"; //Password
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
I hope this article will be helpful to everyone's Java programming.