我的JDBC工具类(1)

1  首先在配置文件(system.properties)中配置上如下内容:

driver=com.microsoft.sqlserver.jdbc.SQLServerDriver

url=jdbc:sqlserver://localhost:1433;databaseName=mp

username=sa

password=mengya

2  写了个SQLDB的工具类

public class SQLDBConnection {

       private InputStream inputstr;

       private Properties pro;

       private static SQLDBConnection sqldb=null;

       //私有构造方法

       private SQLDBConnection(){

              inputstr=this.getClass().getResourceAsStream("/system.properties");

              pro=new Properties();

              try {

                     pro.load(inputstr);

              } catch (IOException e) {

                     e.printStackTrace();

              }

              try {

                     Class.forName(pro.getProperty("driver"));//注册驱动,只注册一次

              } catch (ClassNotFoundException e) {

                     e.printStackTrace();

              }

       }

       //单例模式

       public static SQLDBConnection getSQLDBConnection(){

              if(sqldb==null){

                     synchronized (SQLDBConnection.class) {

                            if(sqldb==null){

                                   sqldb=new SQLDBConnection();

                            }

                     }

              }

              return sqldb;

       }

       //得到与数据库的连接

       public Connection GetConnection(){

              Connection conn=null;

              try {

                     conn=DriverManager.getConnection(pro.getProperty("url"), pro.getProperty("username"), pro.getProperty("password"));

              } catch (SQLException e) {

                     e.printStackTrace();

              }

              return conn;

       }

       //释放资源

       public static void free(ResultSet rs,Statement sta,Connection conn){

              if(rs!=null){

                     try {

                            rs.close();

                     } catch (SQLException e) {

                            e.printStackTrace();

                     }finally{

                            if(sta!=null){

                                   try {

                                          sta.close();

                                   } catch (SQLException e) {

                                          e.printStackTrace();

                                   }finally{

                                          if(conn!=null){

                                                 try {

                                                        conn.close();

                                                 } catch (SQLException e) {

                                                        e.printStackTrace();

                                                 }

                                          }

                                   }

                            }

                     }

              }

       }

}

 

分析:

       这样做法有些浪费,创建了一个Connection只用了一次就关闭,下次用的时候又要打开一新的。没有用到数据库连接池。

你可能感兴趣的:(jdbc,Microsoft)