把操作数据库的方法制作成一个模板吧。你可以参照设计模式中的模板设计。
这样就清楚多了。

解决方案 »

  1.   

    麻烦那位 给改变以下 我想5分钟不到就高定了吧 我也会毛塞顿开的 感谢
      

  2.   

    定义一个类专门处理数据库连接public class Op_CloDB {
      private static Connection conn=null;  public Connection openDB() throws SQLException{
     String userName="root";
             String password="lfj123321";
             String url="jdbc:mysql://192.168.0.7/trjf?useUnicode=true&characterEncoding=gb2312";
             String sDBDriver="com.mysql.jdbc.Driver";
             String sql=null;    try{
    //     
            
             try{
               Class.forName(sDBDriver);
                conn=DriverManager.getConnection(url,userName,password);           //加载驱动
        }
        catch(Exception ex){
          throw new SQLException("不能连接数据库");
        }
        return conn;
      }  public void closeDB()throws SQLException{
        try{
          if (conn!=null){
            conn.close();
            conn=null;
          }
        }
        catch (Exception ex){
          throw new SQLException("释放连接错误");
        }
      }
    }