JSP Simple Access database

Source: Internet
Author: User
Tags stmt

Java code

public class DBHelper {

Private String drivername;

Private String URL;

Private String user;

private String password;

Private Connection Connection;

Private String Createtablesql;

Private String Droptablesql;

public void getconnection () {

if (null = = connection) {

try {

Class.forName (drivername);

Connection = drivermanager.getconnection (URL, user, password);

} catch (ClassNotFoundException | SQLException e) {

E.printstacktrace ();

}

}

}

public void CloseConnection () {

if (null! = connection) {

try {

Connection.close ();

connection = null; Connection.close won ' t set the connection to IS null

System.out.println ("Closed DB connection.");

} catch (SQLException e) {

E.printstacktrace ();

}

}

}

public void CreateTable () {

Getconnection ();

Statement stmt = null;

try {

stmt = Connection.createstatement ();

Stmt.execute (Createtablesql);

SYSTEM.OUT.PRINTLN ("Executed SQL [" + Createtablesql + "] success.");

} catch (SQLException e) {

E.printstacktrace ();

}

CloseConnection ();

}

public void droptable () {

Getconnection ();

Statement stmt = null;

try {

stmt = Connection.createstatement ();

Stmt.execute (Droptablesql);

SYSTEM.OUT.PRINTLN ("Executed SQL [" + Droptablesql + "] success.");

} catch (SQLException e) {

E.printstacktrace ();

}

CloseConnection ();

}

Public String Getdrivername () {

return drivername;

}

public void Setdrivername (String drivername) {

This.drivername = drivername;

}

Public String GetUrl () {

return URL;

}

public void SetUrl (String URL) {

This.url = URL;

}

Public String GetUser () {

return user;

}

public void SetUser (String user) {

This.user = user;

}

Public String GetPassword () {

return password;

}

public void SetPassword (String password) {

This.password = password;

}

Public String Getcreatetablesql () {

return createtablesql;

}

public void Setcreatetablesql (String createtablesql) {

This.createtablesql = Createtablesql;

}

Public String Getdroptablesql () {

return droptablesql;

}

public void Setdroptablesql (String droptablesql) {

This.droptablesql = Droptablesql;

}

}

Java code

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "

pageencoding= "UTF-8"%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">

<title>test connecting MySQL db</title>

<body>

<jsp:usebean id= "DBHelper" class= "Com.jesse.jsp.bean.DBHelper"/>

<jsp:setproperty property= "drivername" name= "DBHelper" value= "Com.mysql.jdbc.Driver"/>

<jsp:setproperty property= "url" name= "DBHelper" value= "jdbc:mysql://192.168.1.104:3306/db_jesse?useunicode= TRUE&CHARACTERENCODING=GBK "/>

<jsp:setproperty property= "user" name= "dbhelper" value= "Jesse"/>

<jsp:setproperty property= "Password" name= "DBHelper" value= "Jesse"/>

<jsp:setproperty property= "Createtablesql" name= "DBHelper" value= "CREATE TABLE test_tbl (id int)"/>

<jsp:setproperty property= "Droptablesql" name= "DBHelper" value= "drop table Test_tbl"/>

<%!

private int id = 0;

%>

<%

System.out.println (this); To check if the servlet is singleton

id++;

Synchronized (Com.jesse.jsp.bean.DBHelper.class) {

Dbhelper.createtable ();

System.out.println (ID + "created the table.");

Thread.Sleep (5000);

Dbhelper.droptable ();

System.out.println (ID + "dropped the table.");

}

%>

</body>

Java code

[Email protected]

Executed SQL [CREATE TABLE TEST_TBL (ID int)] success.

Closed DB connection.

1 created the table.

[Email protected]

Executed SQL [drop table TEST_TBL] success.

Closed DB connection.

2 dropped the table.

Executed SQL [CREATE TABLE TEST_TBL (ID int)] success.

Closed DB connection.

2 created the table.

Executed SQL [drop table TEST_TBL] success.

Closed DB connection.

2 dropped the table.

Note: The intention is to let the log print out which session is executing, see the results ... Long knowledge, <%!. %> looked down, think this statement has reason, temporarily do not drill down: <%%> is the code snippet, after the conversion from JSP to servlet <%%> code is placed in the Serive method, equivalent to Doget () and Dopost () method <%!% > is a JSP declaration that defines properties and methods, and the code in <%!%> after conversion from JSP to servlet is outside the Serive method.


This article from the "11247808" blog, reproduced please contact the author!

JSP Simple Access database

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.