When connecting to a database through JDBC, each database has a different URL format, in order to make it easier for everyone to use, I have provided the following 7 kinds of common database connection sample code, according to the actual needs of the corresponding changes.
1.Oracle Database
Class.forName ("Oracle.jdbc.driver.OracleDriver"). newinstance ();
String url = "Jdbc:oracle:thin: @localhost: 1521:orcle";
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/testdb";
String user = "admin";
String password = "Test";
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=testdb";
String user = "sa";
String password = "Test";
Connection conn = drivermanager.getconnection (URL, user, password);
4. Sybase database
Class.forName ("Com.sybase.jdbc.SybDriver"). newinstance ();
String url = "Jdbc:sybase:tds:localhost:5007/testdb";
Properties sysprops = System.getproperties ();
Sysprops.put ("User", "userid");
Sysprops.put ("Password", "User_password");
Connection conn = drivermanager.getconnection (URL, sysprops);
5.Informix Database
Class.forName ("Com.infoxmix.jdbc.IfxDriver"). newinstance ();
String url = "jdbc:infoxmix-sqli://localhost:1533/testdb:informixserver=myserver;user=testuser;password= Testpassword ";
Connection conn = drivermanager.getconnection (URL);
6.MySQL Database
Class.forName ("Org.gjt.mm.mysql.Driver"). newinstance ();
String url = "jdbc:mysql://localhost/testdb?user=testuser&password=testpassword&useunicode=true& characterencoding=gb2312 ";
Connection conn = drivermanager.getconnection (URL);
7.PostgreSQL Database
Class.forName ("Org.postgresql.Driver"). newinstance ();
String url = "Jdbc:postgresql://localhost/testdb";
String user = "MyUser";
String password = "Test";
Connection conn = drivermanager.getconnection (URL, user, password);
JDBC Various database connection URL key codes