WEB开发网
开发学院WEB开发Jsp 一个操作数据库的Java Bean…… 阅读

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

 2008-01-05 09:36:44 来源:WEB开发网   
核心提示:一个操作数据库的java Bean,有爱好的朋友看一下吧~~package dbconnect; import java.sql.*; public class sqlconnection { String sDbDriver = "sun.jdbc.odbc.JdbcOdbcDriver"

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

Tags:一个 操作 数据库

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接