Jsp jdbc-based database connection instance and jspjdbc database connection
This example describes the JDBC-based database connection class of JSP. We will share this with you for your reference. The details are as follows:
/*** 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 {/ * ** database access URL */private static String url; /*** database driver */private static String driver;/*** database access username */private static String username;/*** database access password */p Rivate static String password;/***** access type */private static String type;/***** data source name */private static String datasource; /*** configuration 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) PropertyResourceBundle. g EtBundle (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 ") user Name = 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 ") ;}}/*** get database connection ** @ 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) {// obtain the data source from JNDI try {// for different application servers, use env to input different Hashtable env = new Hashtable (); // for different application servers, use different Context ctx = new InitialContext (env) for env; // obtain DataSource = (DataSource) as the factory object from the naming system) ctx. lookup (datasource); con = dataSource. getConnection (); connection. set (con); return con;} catch (NamingException e) {E. printStackTrace () ;}} else {// use the JDBC driver to connect to 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 ();} cat Ch (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 File
Copy codeThe Code is 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.