java连接各种数据库

1.Oracle数据库 ---需导入ojdbc14.jar

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数据库  ---需导入db2jcc.jar,db2jcc_license_cu.jar

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数据库

2000版本:---需导入msbase.jar,mssqlserver.jar,msutil.jar

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);

 

2005版本:---需导入sqljdbc4.jar

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();

String url = "jdbc:sqlserver://localhost:1433;DatabaseName=testDB";

String user = "sa";

String password = "test";

Connection conn = DriverManager.getConnection(url, user, password);

 

2008版本:

同2005


4. Sybase数据库 ---需导入jconn2.jar

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数据库  ---需导入的包可以去这里下载对应版本有点多:http://www14.software.ibm.com/webapp/download/search.jsp?go=y&rs=ifxjdbc

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数据库 ---需导入mysql-connector-java-5.0.8-bin.jar包,具体对应mysql版本

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数据库    ---需导入postgresql-8.3-603.jdbc4.jar

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);

 

8.Access数据库ODBC  ---无需导入任何jar包

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
String url="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb");
Connection conn = DriverManager.getConnection(url,"","");
Statement stmtNew=conn.createStatement() ;

 

 

 

你可能感兴趣的:(java连接各种数据库)