数据库操作抽象类

来源:互联网 发布:管家婆导出数据 编辑:程序博客网 时间:2024/06/06 03:15

以后做项目时直接用这个类就可以了,仅仅改一些简单的配置。

 

public  abstract class  BaseJdbcDAO{

 

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

    

         public final static String url = "jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=zf";

 

         public final static String dbName = "sa";

 

         public final static String dbPass = "pwd";

 

 

 

        protected Connection conn = null;

 

        protected Statement stmt = null;

 

        protected PreparedStatement pstmt = null;

 

        protected ResultSet rs = null;

 

 

 

        public Connection openConn(){

 

                try{

 

                      Class.forName(driver);//加载数据库驱动

 

                      conn = DriverManager.getConnection(url,dbName,dbPass);

                   

                }catch(ClassNotFoundException e){

 

                      System.out.println("加载数据库驱动" +driver+ "失败");

 

                      e.printStackTrace();

 

              }catch(SQLExceptio e){

 

                      e.printStackTrace();

              }

}

  

 

        public void closeAll(){

          

              if( rs != null ){

 

                      try{rs.close();}

                      catch(SQLException e){e.printStackTrace();}

             }

              if(pstmt != null){

 

                      try{pstmt.close();}

                      catch(SQLException e){e.printStackTrace();}

             }

 

             if(stmt != null){

 

                      try{stmt .close();}

                      catch(SQLException e){e.printStackTrace();}

             }

 

              if(conn!= null){

 

                      try{conn.close();}

                      catch(SQLException e){e.printStackTrace();}

             }

            

}

 

}

原创粉丝点击