首页 > 学院 > 开发设计 > 正文

一个操作数据库的Java Bean……

2019-11-18 13:40:47
字体:
来源:转载
供稿:网友

  一个操作数据库java Bean,有爱好的朋友看一下吧~~

package dbconnect;

import java.sql.*;

public class sqlconnection
{

String sDbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:sqlconnect"; // sqlconnect 数据源名称;
PRivate Connection conn = null;
private Statement stmt = null;
ResultSet rs = null;

public sqlconnection()
{
try
{
Class.forName(sDbDriver);
}catch(java.lang.ClassNotFoundException e){
System.err.println("sqlconnection():" + e.getMessage());
}
}

public ResultSet executeQuery(String sql)
{
rs = null;
try
{
conn = DriverManager.getConnection(sConnStr);
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}catch (SQLException ex){
System.err.println("aq.executeQuery:" + ex.getMessage());
}
return rs;
}

public void executeUpdate(String sql)
{
stmt = null;
rs = null;
try
{
conn = DriverManager.getConnection(sConnStr);
stmt = conn.createStatement();
stmt.executeQuery(sql);
stmt.close();
conn.close();
}catch (SQLException ex){
System.err.println("aq.executeQuery:" + ex.getMessage());
}
}

public void closeStmt()
{
try
{
stmt.close();
}catch (SQLException ex){
System.err.println("aq.executeQuery:" + ex.getMessage());
}
}

public void closeConn()
{
try
{
conn.close();
}catch (SQLException ex){
System.err.println("aq.executeQuery:" + ex.getMessage());
}
}
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表