Various database connection URL key codes SCS (Chengdu) Technology company Yu Chipeng cfanboy@163.com 14-aug-2005 When connecting to the database through JDBC, each database has a different URL format, for the convenience of everyone, I provide the following common 7 kinds of database connection sample code, please make the appropriate changes according to the actual needs. 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); |