java中连接数据库的BaseDao

两种basedao的写法:

方式一:

public class BaseDao {

private final String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";

private final String url= "jdbc:sqlserver://localhost:1433;databaseName=MyJava";

private final String username = "root";

private final String pwd = "root";

private Connection connection;

private PreparedStatement ps;

protected ResultSet rs;

  public void getConnection(){

    try {

      //加载驱动

      Class.forName(driver);

      // 创建连接对象

      connection = DriverManager.getConnection( url, username,pwd);

    } catch (ClassNotFoundException e) {

    // TODO Auto-generated catch block

             e.printStackTrace();

    } catch (SQLException e) {

        // TODO Auto-generated catch block e.printStackTrace();

           }

        return connection;

     }

  //释放资源(注意:三个关闭要分别用try-catch补获,防止释放资源的时候其中一个没能关闭成功)

 

  public void closeAll(){

      try {

          if (rs != null){

              rs.close();

            }

        } catch (SQLException e) {

            // TODO Auto-generated catch block

                e.printStackTrace(); }

           }

 

      }    

        

       try {

          if (ps != null){

              ps.close();

            }

        } catch (SQLException e) {

            // TODO Auto-generated catch block

                e.printStackTrace(); }

           }

          

       try {

          if (connection != null){

              connection.close();

            }

        } catch (SQLException e) {

            // TODO Auto-generated catch block

                e.printStackTrace(); }

           }

      //查询方式一

    public void executeQuery(String sql , Object[] params){

       try {

      getConnection();

         ps = connection.prepareStatement(sql);

          if(params!= null){

            for (int i = 0; i < params.length; i++) {

                ps.setObject(i+1, params[i]);

               }

            }

             rs = ps.executeQuery();

       } catch (SQLException e) { /

          / TODO Auto-generated catch block

                e.printStackTrace();

                   }

            } 

      //查询方式二

       public void executeQuery1(String sql , Object... params){

          try {

            getConnection();

             ps = connection.prepareStatement(sql);

              if(params!= null){

                 for (int i = 0; i < params.length; i++) {

                      ps.setObject(i+1, params[i]);

                       }

                }

                   rs = ps.executeQuery();

             } catch (SQLException e) {

              // TODO Auto-generated catch block

                  e.printStackTrace();

               }

            }

      //增删改方式一(数组的形式)

      public int executeUpdate(String sql , Object[] params){

                 int result = -1;

              try {

                 getConnection();

                ps = connection.prepareStatement(sql);

                 if(params!= null){

                  for (int i = 0; i < params.length; i++) {

                      ps.setObject(i+1, params[i]);

                    }

                  }

                  result= ps.executeUpdate();

               } catch (SQLException e) {

                // TODO Auto-generated catch block

                    e.printStackTrace();

                }finally{

                    this.closeAll();//关闭所有资源

                  }

                  return result;

                 }

        //增删改方式二

        public int executeUpdate1(String sql , Object... params){

                  int result = -1;

                  try {

                      getConnection();

                      ps = connection.prepareStatement(sql);

                      if(params!= null){

                        for (int i = 0; i < params.length; i++) {

                             ps.setObject(i+1, params[i]);

                           }

                        }

                         result= ps.executeUpdate();

                  } catch (SQLException e) {

                      // TODO Auto-generated catch block

                        e.printStackTrace();

                   }finally{

                      this.closeAll();//关闭所有资源

                      }

                     return result;

               }

     }

  //方式二

  public class BaseDao1 {

    private final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";

    private final String URL= "jdbc:sqlserver://localhost:1433;databaseName=MyJava";

    private final String USERNAME = "sa";

    private final String PWD = "sa";

    protected Connection connection;

    protected PreparedStatement ps;

    public Connection getConnection(){

        try {

          Class.forName(DRIVER);

          // 2在网络中查找数据库,创建连接对象

          connection = DriverManager.getConnection( URL, USERNAME,PWD);

        } catch (ClassNotFoundException e) {

          // TODO Auto-generated catch block e.printStackTrace();

        } catch (SQLException e) {

          // TODO Auto-generated catch block

          e.printStackTrace(); }

          return connection;

         }

       public void closeAll(ResultSet rs, PreparedStatement ps, Connection conn ){

             try {

            // 释放资源 if (rs != null)

              rs.close();

               if (ps != null)

                ps.close();

                if (conn != null)

                  conn.close();

                } catch (SQLException e) { // TODO Auto-generated catch block

                e.printStackTrace();

              }

          }

        public ResultSet executeQuery(String sql , Object[] params){

              ResultSet rs = null;

              try {

                getConnection();

                 ps = connection.prepareStatement(sql);

                if(params!= null){

                   for (int i = 0; i < params.length; i++) {

                      ps.setObject(i+1, params[i]);

                  }

                }

                rs = ps.executeQuery();

               } catch (SQLException e) {

                   // TODO Auto-generated catch block

                  e.printStackTrace(); } return rs;

              }

          public int executeUpdate1(String sql , Object... params){

               int result = -1;

              try {

              getConnection();

               ps = connection.prepareStatement(sql);

              if(params!= null){

                for (int i = 0; i < params.length; i++) {

                     ps.setObject(i+1, params[i]);

                }

               }

                result= ps.executeUpdate();

            } catch (SQLException e) {

                 // TODO Auto-generated catch block

                e.printStackTrace();

             }finally{

                this.closeAll(null,ps,connection);

                 }

             return result;

            }

          }

 

posted @ 2018-01-17 10:49  carrie0608  阅读(992)  评论(0编辑  收藏  举报