欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JSP基于JDBC的数据库连接类实例

程序员文章站 2022-05-18 12:37:27
本文实例讲述了jsp基于jdbc的数据库连接类。分享给大家供大家参考,具体如下: /* * * todo to change the template f...

本文实例讲述了jsp基于jdbc的数据库连接类。分享给大家供大家参考,具体如下:

/*
 *
 * todo to change the template for this generated file go to
 * window - preferences - java - code style - code templates
 */
package com.yanek.test;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.sqlexception;
import java.util.enumeration;
import java.util.hashtable;
import java.util.propertyresourcebundle;
import javax.naming.context;
import javax.naming.initialcontext;
import javax.naming.namingexception;
import javax.sql.datasource;
/**
 * @author administrator
 * 
 * todo to change the template for this generated type comment go to window -
 * preferences - java - code style - code templates
 */
public class database {
 /**
 * 数据库访问url
 */
 private static string url;
 /**
 * 数据库驱动
 */
 private static string driver;
 /**
 * 数据库访问用户名
 */
 private static string username;
 /**
 * 数据库访问口令
 */
 private static string password;
 /**
 * 访问类型
 */
 private static string type;
 /**
 * 数据源名称
 */
 private static string datasource;
 /**
 * 配置文件名称
 */
 private final static string filename = "database";
 private static threadlocal connection = new threadlocal();
 static {
 config();
 }
 private static void config() {
 // 读取系统配置
 propertyresourcebundle resourcebundle = (propertyresourcebundle) propertyresourcebundle
  .getbundle(filename);
 // 将系统设置赋值给类变量
 enumeration enu = resourcebundle.getkeys();
 while (enu.hasmoreelements()) {
  string propertyname = 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
 * @throws sqlexception
 */
 public synchronized static java.sql.connection getconnection()
  throws sqlexception {
 connection con = (connection) connection.get();
 if (con != null && !con.isclosed()) {
  return con;
 }
 if ("pooled".equalsignorecase(type)) {
  // 从jndi中取得数据源
  try {
  // 此处对于不同的应用服务器,对env传入不同
  hashtable env = new hashtable();
  // 此处对于不同的应用服务器,对env传入不同
  context ctx = new initialcontext(env); // 从命名系统中获取 datasource
  // 工厂对象
  datasource datasource = (datasource) ctx.lookup(datasource);
  con = datasource.getconnection();
  connection.set(con);
  return con;
  } catch (namingexception e) {
  e.printstacktrace();
  }
 } else {
  // 直接使用jdbc驱动连接
  try {
  class providerclass = class.forname(driver);
  con = drivermanager.getconnection(url, username, password);
  con.setautocommit(false);
  connection.set(con);
  return con;
  } catch (classnotfoundexception e) {
  e.printstacktrace();
  }
 }
 return null;
 }
 public static void commit() {
 connection con = (connection) connection.get();
 try {
  con.commit();
 } catch (sqlexception e) {
  e.printstacktrace();
 }
 }
 public static void rollback() {
 connection con = (connection) connection.get();
 try {
  con.rollback();
 } catch (sqlexception e) {
  e.printstacktrace();
 }
 }
 public synchronized static void releaseconnection(connection connection) {
 try {
  if (connection != null && !connection.isclosed())
  connection.close();
 } catch (sqlexception e) {
  // todo auto-generated catch block
  e.printstacktrace();
 }
 connection = null;
 }
 public static void main(string[] args) {
 try {
  system.out.println("conn:" + database.getconnection());
 } catch (sqlexception e) {
  // todo auto-generated catch block
  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程序设计有所帮助。