try { rs=stm.executeQuery(str); } catch (SQLException ex) { ex.printStackTrace(); }
来源:学生作业帮助网 编辑:六六作业网 时间:2024/09/11 03:32:24
try { rs=stm.executeQuery(str); } catch (SQLException ex) { ex.printStackTrace(); }
try { rs=stm.executeQuery(str); } catch (SQLException ex) { ex.printStackTrace(); }
try { rs=stm.executeQuery(str); } catch (SQLException ex) { ex.printStackTrace(); }
下面是对sqlserver 2005的连接,用的是java语言
/**
* 此类用于获得数据库连接对象以及关闭
* @author student
*
*/
public class DBConnection {
private static final String DRIVER_CLASS = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String DATABASE_URL = "jdbc:sqlserver://localhost:1035;databaseName=books";
private static final String DATABASE_USER = "sa";
private static final String DATABASE_PASSWORD = "123456";
/**
* 获得一个数据连接对象
* @return
*/
public Connection getConn(){
Connection con = null;
try{
Class.forName(DRIVER_CLASS);
con = DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD);
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}catch(SQLException ex){
ex.printStackTrace();
}
return con;
}
/**
* 关闭数据库相关对象
*/
public void closeConn(ResultSet rs,PreparedStatement ps,Connection con){
try{
if(rs!=null)
rs.close();
if(ps!=null)
ps.close();
if(con!=null)
con.close();
}catch(SQLException ex){
ex.printStackTrace();
}
}
}
下面是对sqlserver 2000的连接,用的是java语言
public class BaseJdbcDAO {
protected Connection conn=null;
protected Statement stmt=null;
protected PreparedStatement pstmt=null;
protected ResultSet rs=null;
protected void openConn(){
Connection aConn=null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
aConn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=epet","123","123");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.conn=aConn;
}
protected void closeAll(){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}