81,094
社区成员
发帖
与我相关
我的任务
分享
package page; //本javabean处于page包中
import java.sql.*;
public class conndb {
private Connection con;
private ResultSet rs;
public static Connection getConnection() throws SQLException {
try {
//连接MS SQL Server数据库
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
return null;
}
return DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=guestinfo","sa","");
//设置连接的数据库名,登陆的用户名和密码
}
//执行SQL语句的查询操作
public ResultSet executeQuery(String sql) {
try {
con = conndb.getConnection();
Statement statement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = statement.executeQuery(sql);
}
catch(SQLException ex)
{
}
return rs;
}
//执行SQL语句的更新操作
public int executeUpdate(String sql)
{
int count = 0;
Statement stmt = null;
try
{
con = conndb.getConnection();
stmt = con.createStatement();
count = stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
}
finally
{
try
{
if(stmt != null)
stmt.close();
if(con != null)
con.close();
}
catch(SQLException ex)
{
System.err.print(ex);
}
}
return count;
}
//释放数据集rs,关闭数据库连接
public void freeRs(ResultSet rs)
{
try
{
if(rs != null)
{
rs.close();
con.close();
}
}
catch(Exception e)
{
}
}
}
return DriverManager.getConnection( "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=guestinfo","sa","");
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("找不到数据源!");
}