JSP基于JDBC的数据库连接类实例
本文实例讲述了JSP基于JDBC的数据库连接类。分享给大家供大家参考,具体如下:
/* * *TODOTochangethetemplateforthisgeneratedfilegoto *Window-Preferences-Java-CodeStyle-CodeTemplates */ packagecom.yanek.test; importjava.sql.Connection; importjava.sql.DriverManager; importjava.sql.SQLException; importjava.util.Enumeration; importjava.util.Hashtable; importjava.util.PropertyResourceBundle; importjavax.naming.Context; importjavax.naming.InitialContext; importjavax.naming.NamingException; importjavax.sql.DataSource; /** *@authorAdministrator * *TODOTochangethetemplateforthisgeneratedtypecommentgotoWindow- *Preferences-Java-CodeStyle-CodeTemplates */ publicclassDatabase{ /** *数据库访问URL */ privatestaticStringurl; /** *数据库驱动 */ privatestaticStringdriver; /** *数据库访问用户名 */ privatestaticStringusername; /** *数据库访问口令 */ privatestaticStringpassword; /** *访问类型 */ privatestaticStringtype; /** *数据源名称 */ privatestaticStringdatasource; /** *配置文件名称 */ privatefinalstaticStringfileName="database"; privatestaticThreadLocalconnection=newThreadLocal(); static{ config(); } privatestaticvoidconfig(){ //读取系统配置 PropertyResourceBundleresourceBundle=(PropertyResourceBundle)PropertyResourceBundle .getBundle(fileName); //将系统设置赋值给类变量 Enumerationenu=resourceBundle.getKeys(); while(enu.hasMoreElements()){ StringpropertyName=enu.nextElement().toString(); if(propertyName.equals("database.url")) url=resourceBundle.getString("database.url"); if(propertyName.equals("database.driver")) driver=resourceBundle.getString("database.driver"); if(propertyName.equals("database.username")) username=resourceBundle.getString("database.username"); if(propertyName.equals("database.password")) password=resourceBundle.getString("database.password"); if(propertyName.equals("database.type")) type=resourceBundle.getString("database.type"); if(propertyName.equals("database.datasource")) datasource=resourceBundle.getString("database.datasource"); } } /** *取得数据库连接 * *@return *@throwsSQLException */ publicsynchronizedstaticjava.sql.ConnectiongetConnection() throwsSQLException{ Connectioncon=(Connection)connection.get(); if(con!=null&&!con.isClosed()){ returncon; } if("pooled".equalsIgnoreCase(type)){ //从JNDI中取得数据源 try{ //此处对于不同的应用服务器,对env传入不同 Hashtableenv=newHashtable(); //此处对于不同的应用服务器,对env传入不同 Contextctx=newInitialContext(env);//从命名系统中获取DataSource //工厂对象 DataSourcedataSource=(DataSource)ctx.lookup(datasource); con=dataSource.getConnection(); connection.set(con); returncon; }catch(NamingExceptione){ e.printStackTrace(); } }else{ //直接使用JDBC驱动连接 try{ ClassproviderClass=Class.forName(driver); con=DriverManager.getConnection(url,username,password); con.setAutoCommit(false); connection.set(con); returncon; }catch(ClassNotFoundExceptione){ e.printStackTrace(); } } returnnull; } publicstaticvoidcommit(){ Connectioncon=(Connection)connection.get(); try{ con.commit(); }catch(SQLExceptione){ e.printStackTrace(); } } publicstaticvoidrollback(){ Connectioncon=(Connection)connection.get(); try{ con.rollback(); }catch(SQLExceptione){ e.printStackTrace(); } } publicsynchronizedstaticvoidreleaseConnection(Connectionconnection){ try{ if(connection!=null&&!connection.isClosed()) connection.close(); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } connection=null; } publicstaticvoidmain(String[]args){ try{ System.out.println("conn:"+Database.getConnection()); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } }
database.property文件
database.driver=com.mysql.jdbc.Driver database.url=jdbc:mysql://localhost/test?user=root&password=root&useUnicode=true&characterEncoding=gbk
希望本文所述对大家jsp程序设计有所帮助。