JSP database connection class instance _jsp programming based on JDBC

Source: Internet
Author: User
Tags commit rollback

The examples in this article describe the database connection classes for JSP based on JDBC. Share to everyone for your reference, specific as follows:

* * * TODO to-change the template for this generated file go to * window-preferences-java-code Style-code Tem
Plates * * 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-* Preferen
 Ces-java-code Style-code Templates/public class Database {/** * database access URL * * private static String URL;
 /** * Database driver */private static String driver;
 /** * Database Access User name */private static String username;
 /** * Database access password * * private static String password;
 /** * Access Type * * private static String type;
 /** * Data Source name */private static String datasource; /** * Config file name * * Private Final static String fileName = "Database";
 private static ThreadLocal connection = new ThreadLocal ();
 Static {config (); private static void Config () {//Read system configuration propertyResourceBundle ResourceBundle = (propertyresourcebundle) propertyres
 Ourcebundle. Getbundle (FileName);
 Assign the system settings to the class variable 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"); /** * Obtain Database connection * * @return * @throws SQLException/public synchronized static Java.sql.Connection GetConnect
 Ion () throws SQLException {Connection con = (Connection) connection.get ();
 if (Con!= null &&!con.isclosed ()) {return con; } if ("Pooled". Equalsignorecase (Type)) {//Get data source from Jndi try {//here for different application servers, Env is passed into different Hashtable env = new Hashta
  BLE (); Here for different application servers, the env is passed into different context ctx = new InitialContext (env);
  Obtain DataSource//factory objects from the naming system DataSource DataSource = (DataSource) ctx.lookup (DataSource);
  con = datasource.getconnection ();
  Connection.set (con);
  return con;
  catch (Namingexception e) {e.printstacktrace ();
  } else {//directly using the JDBC driver to connect 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 ();
 The 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 &&amp
 ;!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 file

Copy Code code as follows:
Database.driver=com.mysql.jdbc.driver
Database.url=jdbc:mysql://localhost/test?user=root&password=root&useunicode=true&characterencoding =gbk

I hope this article will help you with JSP program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.