日期:2014-05-16 浏览次数:20468 次
jdbc连接步骤(以oracle为例):
1,加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
?
2, 建立连接
Connection cn = DriverManager.getConnection("jdbc:oracle:thin:@10.49.0.6:1521:orcl", "tvcms_dev", "tvcms_dev");
?
3, 建立 statement
Statement st = cn.createStatement();
?
4, 执行 sql,获得结果集
ResultSet rs = st.excuteQuery(sql);
?
5, 处理结果集
while(rs.next()){
?rs.getObject("字段名")
}
?
6, 关闭资源
finally{
?rs = null;
?st?= null;
?cn = null;
}
?
?
-------------连接大全-----------
一、DB2?
Class.forName("Com.ibm.db2.jdbc.net.DB2Driver");?
String url="jdbc:db2://dburl:port/DBname"?
cn = DriverManager.getConnection( url, sUsr, sPwd );
?
二、Microsoft SQLServer(microsoft)?
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver" );?
cn = DriverManager.getConnection( "jdbc:microsoft:sqlserver://DBServerIP:1433;databaseName=master", sUsr, sPwd );?
?
三、Sybase(jconn2.jar)
Class.forName( "com.sybase.jdbc2.jdbc.SybDriver" );?
cn = DriverManager.getConnection( "jdbc:sybase:Tds:DBServerIP:2638", sUsr, sPwd );?
?
四、MySQL(mm.mysql-3.0.2-bin.jar)
Class.forName( "org.gjt.mm.mysql.Driver" );?
cn = DriverManager.getConnection( "jdbc:mysql://DBServerIP:3306/myDatabaseName", sUsr, sPwd );?
?
五、PostgreSQL(pgjdbc2.jar)
Class.forName( "org.postgresql.Driver" );?
cn = DriverManager.getConnection( "jdbc:postgresql://DBServerIP/myDatabaseName", sUsr, sPwd );?
?
六、Oracle(classes12.jar)?
Class.forName( "oracle.jdbc.driver.OracleDriver" );?
cn = DriverManager.getConnection( "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd );?