好的!
package com.hzs.order;import java.io.IOException;
import java.sql.*;
import java.util.Vector;
import java.util.*;public class DBHandle {
// Field
  private Vector result = null;
  private Connection conn = null;// Construct
  public DBHandle() {
  }// Connect to database
  public void connect() throws DBException, IOException {
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
      conn = DriverManager.getConnection("jdbc:oracle:thin:system/[email protected]:1521:mydb");
    } catch(ClassNotFoundException E) {
      throw new DBException("Connnect: " + String.valueOf(E.getMessage()));
    } catch(SQLException E) {
      throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
    } catch(Exception E) {
      throw new DBException("Exception: " + String.valueOf(E.getMessage()));
    }
  }  public void transaction() throws DBException, IOException {
    try {
      conn.setAutoCommit(false);
    } catch(SQLException E) {
      throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
    } catch(Exception E) {
      throw new DBException("Exception: " + String.valueOf(E.getMessage()));
    }
  }// Executes a SQL statement that returns a single ResultSet.
  public Vector select(String strQuery) throws DBException, IOException {
    result = new Vector();    try {
      Statement stmt = conn.createStatement();
      ResultSet rs = stmt.executeQuery(strQuery);
      ResultSetMetaData rsmd = rs.getMetaData();
      int FieldNum = rsmd.getColumnCount();      while (rs.next()) {
        for (int i = 1; i <= FieldNum; i ++)
          result.addElement(rs.getString(i));
      }      rs.close();
      stmt.close();
    } catch(SQLException E) {
      throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
    } catch(Exception E) {
      throw new DBException("Exception: " + String.valueOf(E.getMessage()));
    }
    return result;
  }// Executes an SQL INSERT, UPDATE or DELETE statement.
  public int update(String strUpdate) throws DBException, IOException {
    int stmtInt=-1;
try {
      Statement stmt = conn.createStatement();
      stmtInt = stmt.executeUpdate(strUpdate);
      stmt.close();
    } catch(SQLException E) {
      throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
    } catch(Exception E) {
      throw new DBException("Exception: " + String.valueOf(E.getMessage()));
    }
return stmtInt;
  }// commit
  public void commit() throws DBException, IOException {
    try {
      conn.commit();
    } catch(SQLException E) {
      throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
    } catch(Exception E) {
      throw new DBException("Exception: " + String.valueOf(E.getMessage()));
    }
  }// rollback
  public void rollback() throws DBException, IOException {
    try {
      conn.rollback();
    } catch(SQLException E) {
      throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
    } catch(Exception E) {
      throw new DBException("Exception: " + String.valueOf(E.getMessage()));
    }
  }// close
  public void close() throws DBException, IOException {
    try {
      conn.close();
    } catch(SQLException E) {
      throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
    } catch(Exception E) {
      throw new DBException("Exception: " + String.valueOf(E.getMessage()));
    }
  }  public static String toChinese(String strValue) {
    try {
      if (strValue == null)
        return null;
      else
        return new String(strValue.getBytes("8859_1"), "gb2312");
    } catch(Exception e) {
      return null;
    }
  }  public static float toFloat(String str){
    return Float.parseFloat(str);
  }
}

解决方案 »

  1.   

    好的!
    package com.hzs.order;import java.io.IOException;
    import java.sql.*;
    import java.util.Vector;
    import java.util.*;public class DBHandle {
    // Field
      private Vector result = null;
      private Connection conn = null;// Construct
      public DBHandle() {
      }// Connect to database
      public void connect() throws DBException, IOException {
        try {
          Class.forName("oracle.jdbc.driver.OracleDriver");
          conn = DriverManager.getConnection("jdbc:oracle:thin:system/[email protected]:1521:mydb");
        } catch(ClassNotFoundException E) {
          throw new DBException("Connnect: " + String.valueOf(E.getMessage()));
        } catch(SQLException E) {
          throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
        } catch(Exception E) {
          throw new DBException("Exception: " + String.valueOf(E.getMessage()));
        }
      }  public void transaction() throws DBException, IOException {
        try {
          conn.setAutoCommit(false);
        } catch(SQLException E) {
          throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
        } catch(Exception E) {
          throw new DBException("Exception: " + String.valueOf(E.getMessage()));
        }
      }// Executes a SQL statement that returns a single ResultSet.
      public Vector select(String strQuery) throws DBException, IOException {
        result = new Vector();    try {
          Statement stmt = conn.createStatement();
          ResultSet rs = stmt.executeQuery(strQuery);
          ResultSetMetaData rsmd = rs.getMetaData();
          int FieldNum = rsmd.getColumnCount();      while (rs.next()) {
            for (int i = 1; i <= FieldNum; i ++)
              result.addElement(rs.getString(i));
          }      rs.close();
          stmt.close();
        } catch(SQLException E) {
          throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
        } catch(Exception E) {
          throw new DBException("Exception: " + String.valueOf(E.getMessage()));
        }
        return result;
      }// Executes an SQL INSERT, UPDATE or DELETE statement.
      public int update(String strUpdate) throws DBException, IOException {
        int stmtInt=-1;
    try {
          Statement stmt = conn.createStatement();
          stmtInt = stmt.executeUpdate(strUpdate);
          stmt.close();
        } catch(SQLException E) {
          throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
        } catch(Exception E) {
          throw new DBException("Exception: " + String.valueOf(E.getMessage()));
        }
    return stmtInt;
      }// commit
      public void commit() throws DBException, IOException {
        try {
          conn.commit();
        } catch(SQLException E) {
          throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
        } catch(Exception E) {
          throw new DBException("Exception: " + String.valueOf(E.getMessage()));
        }
      }// rollback
      public void rollback() throws DBException, IOException {
        try {
          conn.rollback();
        } catch(SQLException E) {
          throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
        } catch(Exception E) {
          throw new DBException("Exception: " + String.valueOf(E.getMessage()));
        }
      }// close
      public void close() throws DBException, IOException {
        try {
          conn.close();
        } catch(SQLException E) {
          throw new DBException("SQLException: " + String.valueOf(E.getMessage()));
        } catch(Exception E) {
          throw new DBException("Exception: " + String.valueOf(E.getMessage()));
        }
      }  public static String toChinese(String strValue) {
        try {
          if (strValue == null)
            return null;
          else
            return new String(strValue.getBytes("8859_1"), "gb2312");
        } catch(Exception e) {
          return null;
        }
      }  public static float toFloat(String str){
        return Float.parseFloat(str);
      }
    }
      

  2.   

    这个方法我也想过,但是觉得用bean做灵活性太差了,因为你要对不同的表,不同的字段进行控制但是如果表的名字和字段名字以及所要做的操作作为参数传入理论上是可以的
    单是我没有这么试过我还是比较喜欢数据库连接放在bean里,其他的操作放在jsp里,但是这样做好像不大规范......好像高手放在bean里 你试试传参数行不行,如果行的话告诉我一声关注!
      

  3.   

    package com.hzs.order;public class DBException extends Exception {
      public DBException(String desc) {
        super(desc);
      }
    }
      

  4.   

    这是我的程序,请问怎么才能改的修改和添加都好用啊,谢谢了,请指点一下,100分必给!!!!!!!!
    package com.acetech.ramon.web;
    import java.sql.*;
    public class User {  public User() {
      }
      public static void main(String[] args) throws Exception{
        //User user = User.findById("2");
        //System.out.println(user.getUid());
        //System.out.println(user.getName());
        //System.out.println(user.getPassword());
        //boolean falg = User.deleteById("2");
        //System.out.println(falg);
        User.updateById("55","78","78900");
    //    System.out.println(user2.getUid());
    //    System.out.println(user2.getName());
    //    System.out.println(user2.getPassword());
      }
      public void setUid(String uid) {
        this.uid = uid;
      }
      public String getUid() {
        return uid;
      }
      public void setName(String name) {
        this.name = name;
      }
      public String getName() {
        return name;
      }
      public void setPassword(String password) {
        this.password = password;
      }
      public String getPassword() {
        return password;
      }
    public static void updateById(String uid,String name, String password) throws Exception{
     Connection conn = DBManager.getConnection();
     PreparedStatement ps = conn.prepareStatement("UPDATE user SET name=?,password=? WHERE uid=?");
         ps.setString(1,uid);
         ps.setString(2,name);
         ps.setString(3,password);
         int i = ps.executeUpdate();
         //User user = new User();
    //     if (rs.next()){
    //     user.setUid(rs.getString(1));
    //     user.setName(rs.getString(2));
    //     user.setPassword(rs.getString(3));
    //     }
    //     rs.close();
           ps.close();
           conn.close();
       //  return user;
     }
     public static User insertid(String uid) throws Exception {
          Connection conn = DBManager.getConnection();
          PreparedStatement ps = conn.prepareStatement("INSERT INTO user (uid,name,password) VALUES('999','789','889')");
          //ps.setString(1,uid);
          ResultSet rs = ps.executeQuery();
          User user = new User();
          if (rs.next()){
          user.setUid(rs.getString(1));
          user.setName(rs.getString(2));
          user.setPassword(rs.getString(3));
          }
          rs.close();
          ps.close();
          conn.close();
          return user;
     }/******************************************************************************/  private String uid;
      private String name;
      private String password;
    }
      

  5.   

    package com.acetech.ramon.web;
    import java.sql.*;
    public class User {  public User() {
      }
      public static void main(String[] args) throws Exception{
        //User user = User.findById("2");
        //System.out.println(user.getUid());
        //System.out.println(user.getName());
        //System.out.println(user.getPassword());
        //boolean falg = User.deleteById("2");
        //System.out.println(falg);
        User.updateById("55","78","78900");
    //    System.out.println(user2.getUid());
    //    System.out.println(user2.getName());
    //    System.out.println(user2.getPassword());
      }
      public void setUid(String uid) {
        this.uid = uid;
      }
      public String getUid() {
        return uid;
      }
      public void setName(String name) {
        this.name = name;
      }
      public String getName() {
        return name;
      }
      public void setPassword(String password) {
        this.password = password;
      }
      public String getPassword() {
        return password;
      }
    public static void updateById(String uid,String name, String password) throws Exception{
    //add 
    Class.forName("oracle.jdbc.driver.OracleDriver");
    //end
    Connection conn = DBManager.getConnection();
     PreparedStatement ps = conn.prepareStatement("UPDATE user SET name=?,password=? WHERE uid=?");
    //modifly     
         ps.setString(1,name);
         ps.setString(2,password);
         ps.setString(3,uid);
    //end
         int i = ps.executeUpdate();
         //User user = new User();
    //     if (rs.next()){
    //     user.setUid(rs.getString(1));
    //     user.setName(rs.getString(2));
    //     user.setPassword(rs.getString(3));
    //     }
    //     rs.close();
           ps.close();
           conn.close();
       //  return user;
     }
     public static User insertid(String uid) throws Exception {
          Connection conn = DBManager.getConnection();
          PreparedStatement ps = conn.prepareStatement("INSERT INTO user (uid,name,password) VALUES('999','789','889')");
    //你怎么不设置ID啊!?      
    //ps.setString(1,uid);
    //使用executeUpdate()
          int i = ps.executeUpdate();
          User user = new User();
     //我不知道你下面的代码是做什么的,为什么要设置啊!?
    //如果一定要设置你可以在这里再查找!
          if (rs.next()){
          user.setUid(rs.getString(1));
          user.setName(rs.getString(2));
          user.setPassword(rs.getString(3));
          }
          rs.close();
          ps.close();
          conn.close();
          return user;
     }/******************************************************************************/  private String uid;
      private String name;
      private String password;
    }
      

  6.   

    pengji(彭乃超) 
    首先非常感谢你,但是昨天那程序的修改段我作好了,马上问你一下增加段的好吗,我总是编译通不过,说是ra没有定义,马上给出程序段,再次感谢
      

  7.   

    package com.acetech.ramon.web;
    import java.sql.*;
    public class User {  public User() {
      }
      public static void main(String[] args) throws Exception{
          User.insertUid("1","ding","sanguo");
     }
      public void setUid(String uid) {
        this.uid = uid;
      }
      public String getUid() {
        return uid;
      }
      public void setName(String name) {
        this.name = name;
      }
      public String getName() {
        return name;
      }
      public void setPassword(String password) {
        this.password = password;
      }
      public String getPassword() {
        return password;
      }
    //增加
    public static User insertUid(String uid,String name,String password) throws Exception {
          Connection conn = DBManager.getConnection();
          PreparedStatement ps = conn.prepareStatement("SELECT uid from user order by uid desc");
          ResultSet rs = ps.executeQuery();
          User user = new User();
            if (rs.next()){
            String ra=rs.getString(1);
            }
    //        else ra==0;
    //我想在这里判断一下rs,如果失败,就赋给ra一个0,
            int temp=Integer.parseInt(ra);
            temp = temp + 1;
            String ra1=Integer.toString(temp);
            rs.close();
            ps.close();
    //就是这段程序了,请帮我看一下,
          PreparedStatement ps1 = conn.prepareStatement("INSERT INTO user VALUES(?,?,?)");
          ps1.setString(1,uid);
          ps1.setString(2,name);
          ps1.setString(3,password);
          int i = ps1.executeUpdate();
           ps1.close();
           conn.close();
     }/******************************************************************************/  private String uid;
      private String name;
      private String password;
    }