
System.out.println(rs.getInt(id))
}
5:关闭数据源:rs.close();
下面是连接各种数据库的方法:
1、Oracle8/8i/9i数据库(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance()
String url="jdbc:oracle:thin:@localhost:1521:orcl"
//orcl为数据库的SID
String user="test"
String password="test"
Connection conn= DriverManager.getConnection(url,user,password)
2、DB2数据库
Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance()
String url="jdbc:db2://localhost:5000/sample"
//sample为你的数据库名
String user="admin"
String password=""
Connection conn= DriverManager.getConnection(url,user,password)
3、Sql Server7.0/2000数据库
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance()
String url="jdbc:microsoft:sqlserver://localhost:1433DatabaseName=mydb"
//mydb为数据库
package com.bin.struts.utilimport java.sql.Connection
import java.sql.DriverManager
import java.sql.SQLException
public class DBConn2000 {
public static Connection getConn(){
String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"
String url = "jdbc:microsoft:sqlserver://localhost:1433DatabaseName=//数据库名字"
String user = "sa"
String password = "123"
try {
Class.forName(driver)
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace()
}
try {
return DriverManager.getConnection(url,user,password)
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
return null
}
}
public static Connection close(){
return null
}
}
这是2000的连接方法
这是2005的连接串
jdbc:sqlserver://localhost:1433databasename=//数据库
com.microsoft.sqlserver.jdbc.SQLServerDriver
2000数据库需要打SP4补丁.
并且两个都需要专门的JDBC驱动的
String s strURL="jdbc:odbc:student"// 数据库标识名, 其中student为我们数据库的名字String user//数据库的登录名
String password//数据库的登录密码
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
Connection conn=DriverManager.getConnection(strURL,user,password)//获取连接
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE)//可滚动的
我再给你添加这种odbc的连接方式,即先用odbc数据源管理将数据库添加为用户数据源,程序连接的时候连接数据源就行,因为有的数据库需要专门的调用驱动,安装和调试不易,所以用odbc做一个中转,请结合参考资料理解
这几种就是java连接各类常用数据库的方法了
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)