一个操作数据库的Java Bean……
发表于:2007-07-04来源:作者:点击数:
标签:
一个操作 数据库 的 Java Bean,有兴趣的朋友看一下吧~~ package dbconnect; import java . sql .*; public class sqlconnection { String sDbDriver = sun.jdbc.odbc.JdbcOdbcDriver; String sConnStr = jdbc:odbc:sqlconnect; // sqlconnect 数据源名称; p
一个操作
数据库的
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());
}
}
}
原文转自:http://www.ltesting.net