}catch(SQLException se){
throw new SQLException("RUL class not found\n"+se.toString());
}
}catch(ClassNotFoundException ce){
throw new SQLException("Driver class not found\n" + ce.toString());
}
return conn;
}
/*关闭ResultSet对象*/
public static void closeResultSet(ResultSet rs){
try{
if(rs!=null)
rs.close();
}catch(SQLException pe){
System.out.println("ResultSet close ERROR: "+pe.toString());
}
}
/*关闭PrepareStatement 对象*/
public static void closePreparedStatement(PreparedStatement pstmt) {
try{
if(pstmt!=null)
pstmt.close();
}catch(SQLException pe){
System.out.println("PreparedStatement close ERROR: "+pe.toString());
}
}
/*关闭Statement 对象*/
public static void closeStatement(Statement stmt){
try{
if(stmt!=null)
stmt.close();
}catch(SQLException pe){
System.out.println("Statement close ERROR: "+pe.toString());
}
}
/*关闭Connection 对象*/
public static void closeConnection(Connection conn){
try{
if(conn!=null)
conn.close();
}catch(SQLException pe){
System.out.println("Connection close ERROR: "+pe.toString());
}
}
/**
*
* @param conn
* @param p
* @param rs
*/
public static void closeCPR(Connection conn,PreparedStatement p,ResultSet rs){
try{
if (rs != null)
rs.close();
if (p != null)
p.close();
if (conn != null)
conn.close();
}catch(SQLException pe){
System.out.println("Connection close ERROR: "+pe.toString());
}
import java.sql.*;
import java.util.*;
public class JdbcTest{
public static final String ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver";
public static final String URL = "jdbc:oracle:thin:@192.168.0.20:1521:tarena";
//jdbc:协议:子协议:@host:oracle端口:sid
public static final String USER = "scott";
public static final String PASSWORD = "tiger";
public static void main(String[] args) throws ClassNotFoundException,SQLException{
JdbcTest jt = new JdbcTest();
//jt.create(2,"hehe","hehe@tarena.com.cn");
//jt.findAll();
int id = 2;
Userinfo user = jt.find(id);
if(user!=null){
System.out.println(user);
}else{
System.out.println("user not found:id="+id);
}
//delete by id
jt.delete(id);
List users = jt.findAll();
System.out.println("there are "+users.size()+" users");
Iterator it = users.iterator();
String email = "haha@sohu.com";
Userinfo userFound = null;
while(it.hasNext()){
user = (Userinfo)it.next();
if(user.getEmail().equals(email)){
userFound = user;
}
System.out.println(user);
}
System.out.println("-------------");
if(userFound!=null){
System.out.println(userFound);
}
}
public static Connection getCon(){
Connection con = null;
try{
Class.forName(ORACLE_DRIVER);
con = DriverManager.getConnection(URL,USER,PASSWORD);
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
return con;
}
public Userinfo find(int id){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
Userinfo user = null;
try{
//2、得到连接
conn = getCon();
//3、得到陈述对象
stmt = conn.createStatement();
String sql = "select * from tb_userinfo where id="+id;
要获取连接 URL 参数的完整列表,请参见 Microsoft SQL Server 2000 JDBC 驱动程序 HTML 帮助,或参见联机指南。请参见“连接字符串属性”一节。
用于测试连接的代码示例
下面的代码示例尝试连接到数据库,并显示数据库名称、版本和可用编目。请用您服务器的值替换代码中的服务器属性:
import java.*;
public class Connect{
private java.sql.Connection con = null;
private final String url = "jdbc:microsoft:sqlserver://";
private final String serverName= "localhost";
private final String portNumber = "1433";
private final String databaseName= "pubs";
private final String userName = "user";
private final String password = "password";
// Informs the driver to use server a side-cursor,
// which permits more than one active statement
// on a connection.
private final String selectMethod = "cursor";
如果此代码运行成功,其输出结果应类似于以下内容:
Connection Successful!
Driver Information
Driver Name:SQLServer
Driver Version: 2.2.0022
Database Information
Database Name:Microsoft SQL Server
Database Version:Microsoft SQL Server 2000 - 8.00.384 (Intel X86)
May 23 2001 00:02:52
Copyright (c) 1988-2000 Microsoft Corporation
Desktop Engine on Windows NT 5.1 (Build 2600: )
有关排除连接故障的基本信息
下面是尝试连接到 SQL 服务器时常见的错误信息:
java.sql.SQLException:[Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Login failed for user 'user'.Reason:Not associated with a trusted SQL Server connection.
如果将 SQL Server 2000 的验证模式设置为“Windows 验证模式”,则会出现此错误信息。Microsoft SQL Server 2000 JDBC 驱动程序不支持使用 Windows NT 验证进行连接。您必须将 SQL Server 的验证模式设置为“混合模式”,该模式既允许 Windows 验证,也允许 SQL Server 验证。
java.sql.SQLException:[Microsoft][SQLServer 2000 Driver for JDBC]This version of the JDBC driver only supports Microsoft SQL Server 2000. You can either upgrade to SQL Server 2000 or possibly locate another version of the driver.
当您尝试连接到 SQL Server 2000 以前的 SQL Server 版本时,则会出现此错误信息。Microsoft SQL Server 2000 JDBC 驱动程序仅支持与 SQL Server 2000 进行连接。