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

类别:Java 点击:0 评论:0 推荐:

一个操作数据库的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://com.8s8s.com/it/it14218.htm