db.update(sql);这语句之前不需要调用db.closeStm(),因为第二次的stm会覆盖掉第一次的stm。你运行一下或者写个test测试一下就知道了

解决方案 »

  1.   

    其实在query和update方法里面都有stm = conn.createStatement()可以移到getConnection方法里面,你在调用DBsuccess的getConnection方法建立连接的时候就初始化了stm,后面再执行多少次sql都没关系了,最后在调用数据库close语句就可以了。
    DBconnect修改如下:import java.sql.*;public class DBconnect {
    private final String driver = "net.sourceforge.jtds.jdbc.Driver";
    private final String url = "jdbc:jtds:sqlserver://localhost:1433/sql";
    private final String usename = "ers";
    private final String password = "123";
    private Connection conn = null;
    private Statement stm = null;
    private ResultSet res = null; public boolean getConnection() {
    try {
    Class.forName(driver).newInstance();
    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
    boolean resave = false;
    try {
    conn = DriverManager.getConnection(this.url, this.usename,
    this.password);
    stm = conn.createStatement();
    resave = true;
    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
    return resave;
    } public boolean update(String sql) {
    boolean resave = false;
    try {
    stm.execute(sql);
    resave = true;
    sql = null;
    } catch (Exception e) {
    }
    return resave;
    } public void query(String sql) {
    try {
    res = stm.executeQuery(sql);
    sql = null;
    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
    } public boolean next() {
    boolean resave = false;
    try {
    if (res.next()) {
    resave = true;
    }
    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
    return resave;
    } public String getValue(String field) {
    String value = null;
    try {
    if (res != null) {
    value = res.getString(field);
    }
    } catch (Exception e) { }
    field = null;
    return value;
    } public void closeRs() {
    try {
    if (res != null) {
    res.close();
    }
    } catch (Exception e) {
    }
    } public void closeStm() {
    try {
    if (stm != null) {
    stm.close();
    }
    } catch (Exception e) {
    }
    } public void closeConn() {
    try {
    if (conn != null) {
    conn.close();
    }
    } catch (Exception e) {
    }
    }
    }
      

  2.   

    注册driver的语句Class.forName(driver)只需要执行一次,放static快就够了。
      

  3.   

    最好是把与数据库连接和常用的操作方法写在一个通用的JavaBean中,在其他类调用的OK,实现了继承,利于程序维护和扩展