当前位置导航:炫浪网>>网络学院>>网页制作>>JSP教程

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

一个操作数据库的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());
}
}
}


相关内容
赞助商链接