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程序设计有所帮助。
热门推荐
10 爱情最美好祝福语简短
11 爱情寄语简短祝福语大全
12 宝宝新生入学祝福语简短
13 店家搬家祝福语大全简短
14 乔迁当面祝福语简短大气
15 姑娘生日祝福语简短的话
16 师傅退休蛋糕祝福语简短
17 送去考试祝福语简短英文
18 疫情五一快乐祝福语简短