日期:2014-05-16  浏览次数:20496 次

java 连接数据库的一些代码
Java数据库连接接口(JDBC)是Java里定义的一套用于数据库连接和操作的API的集合。由不同的数据库厂商提供这套接口的实现类,对于java程序员来说,程序员不需要关心数据库的底层的实现,统一的调用JDBC里的抽象出来的API来完成数据库的访问功能,在Data access的功能和接口定义中,java是在别的语言里实现的比较理想的, 作为程序员需要的是,知道每个数据库的不同的JDBC的实现,也就是driver,以及对数据库的URL的描述,其他的一切也就是通过统一的调用来实现了,本文章收集了主要的数据库厂商提供的driver和url,以便大家方便的对各种数据库进行连接的操作。 


JDBC里统一的使用方法 
Class.for(jdbcDriverName); 
Connection conn= DriverManager.getConnection(url,user,password); 
这里的接口是统一的,不同的是每种数据库提供的jdbcDriver和Url不同 

我们一起来看看吧 

MySQL  

Class.forName("com.mysql.jdbc.Driver"); 
Connection con = DriverManager.getConnection("jdbc:mysql://host:port/database","user","password"); 

Oracle数据库(thin模式) 

Class.forName("oracle.jdbc.driver.OracleDriver"); 
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@host:port:databse","user","password"); 

Timesten数据库(thin模式) 

Class.forName("com.timesten.jdbc.TimesTenDriver"); 
Connection con = DriverManager.getConnection("jdbc:timesten:client:dsn=dsnname") 

Microsoft SQL Server  
Class.forName("net.sourceforge.jtds.jdbc.Driver "); 
Connection con = DriverManager.getConnection("jdbc:jtds:sqlserver://host:port/database","user","password"); 
or 
Connection con = DriverManager.getConnection("jdbc:jtds:sybase://host:port/database","user","password"); 

DB2  
Class.forName("com.ibm.db2.jdbc.app.DB2Driver "); 
Connection conn= DriverManager.getConnection("jdbc:db2://localhost:5000/sample",user,password); 

Firebird  
Class.forName("org.firebirdsql.jdbc.FBDriver"); 
Connection con = DriverManager.getConnection("jdbc:firebirdsql://host:port/fdb_file_path","user","password"); 

PointBase(Server)  
Class.forName("com.pointbase.jdbc.jdbcUniversalDriver"); 
Connection con = DriverManager.getConnection("jdbc:pointbase:server://host:port/databasename","user","password"); 

HSQLDB(Server)  
Class.forName("org.hsqldb.jdbcDriver"); 
Connection con = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost:port/mydb","user","password"); 

Derby  
这个少有人听说吧,cloudscape 应该听说过吧,该项目由此衍生 
Class.forName("org.apache.derby.jdbc.ClientDriver"); 
Connection con = DriverManager.getConnection("jdbc:derby://host:port//mydb","user","password"); 

Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); 
Connection conn = DriverManager.getConnection("jdbc:derby:mynewDB", p); 

cloudscape  
Class.forName("com.cloudscape.core.JDBCDriver"); 
Connection conn = DriverManager.getConnection("jdbc:cloudscape:<DB>"); 

Class.forName("com.cloudscape.rmiJdbc.RJDriver"); 
Connection conn = DriverManager.getConnection("jdbc:rmi://<HOST>:<PORT>/jdbc:cloudscape:<DB>", user, passoword); 

Postgresql  
Class.forName("org.postgresql.Driver"); 
Connection con=DriverManager.getConnection("jdbc:postgresql://host:port/database","user","password"); 

Informix  
Class.forName("com.informix.jdbc.IfxDriver").newInstance(); 
Connection con=DriverManager.getConnection("jdbc:informix-sqli://host:port/myDB:INFORMIXSERVER=myserver","user","password"); 

Access  
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ; 
Connection con=DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=mdb的文件路径","user","password"); 

Sybase  
Class.forName("com.sybase.jdbc2.jdbc.SybDriver").newInstance(); 
Connection con=DriverManager.getConnection("jdbc:sybase:Tds:host:port?ServiceName=databasename","user","password"); 

==================附加======================  
IBM AS400主机在用的JDBC语法  
Class.forName("com.ibm.as400.access.AS400JDBCConnection"); 
Connection con = DriverManager.getConnection("jdbc:as400://IP","user","passwor"); 

Sapdb  
Class.forName("com.sap.dbtech.jdbc.DriverSapDB"); 
Connection con = DriverManager.getConnection("jdbc:sapdb://host:port/DB_NAME","user","passwor"); 

Progress  
Class.forName("com.progress.sql.jdbc.JdbcProgressDriver"); 
Connection con = DriverManager.getConnection("jdbc:JdbcProgress:T:dbserver:3305:liveappl","user","passwor"); 

Mimer SQL Local  
Class.forName("com.mimer.jdbc.Driver"); 
Connection con = Driv