JSP簡單訪問資料庫

來源:互聯網
上載者:User

標籤:資料庫   java   

 Java代碼

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 be 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代碼

<%@ 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">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Test connecting mysql db</title>

</head>

<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>

</html>

  Java代碼

[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:本意是要讓log列印出來是哪個session在執行,看到結果...又長知識了 <%! %> 查了下,覺得這種說法有道理,暫不深究: <%%>是程式碼片段,在由jsp轉換成Servlet後 <%%>中的代碼是放在serive方法中,相當於doGet()和doPost()方法 <%!%>是jsp聲明,用來定義屬性和方法的,在由jsp轉換成Servlet後 <%!%>中的代碼是放serive方法之外的.


本文出自 “11247808” 部落格,轉載請與作者聯絡!

JSP簡單訪問資料庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.