現今全部的 Web都是基於數據庫的,可見數據庫在Web中的開發時如此的重要。然而基本上全部的網站都是要求客戶登錄的時候都必須事先註冊帳號,這樣才能進入網頁,訪問其中的頁面,資源。。開發基於數據庫的Web程序時,涉及到多個用戶同時請求與數據庫創建"鏈接"的問題,但多用戶併發處理目前仍是一個難點。該文在對傳統數據鏈接模式的分析基礎上,採用了一種基於對象的數據鏈接池的設計方案。經過鏈接對象放置預先創建的若干"鏈接",根據必定的策略管理這些"鏈接",防止"鏈接"過多內存開銷過大,及鏈接排隊過長問題。並用JAVA實現了該方案,在實際應用中提供了一個高效的鏈接管理策略,提升了Web數據庫應用系統的性能。
一:在此我經過一個實際的例子談談我最近最Web得心得,首先咱們創建一個javaWeb項目在Myeclipse下。首先創建好數據庫鏈接,即創建一個Vo對象User類,以下:html
package vo; java
/* 用戶名稱:<input type="text" name="username"><br>sql
用戶密碼:<input type="password" name="password"><br> 數據庫
用戶愛好:<input type="checkbox" name="hobby" value="1">游泳 瀏覽器
<input type="checkbox" name="hobby" value="2">足球<br> 併發
用戶性別:<input type="radio" name="gender" value="male">男 app
<input type="radio" name="gender" value="female">女 eclipse
<input type="radio" name="gender" value="secret">保密<br> jsp
用戶職位:<select name="position">post
<option value="CEO">CEO</option>
<option value="CFO">CFO</option>
<option value="CTO">CTO</option>
</select><br>
用戶簡歷:<textarea rows="5" cols="20" name="resume"></textarea>
<input type="submit" value="註冊">
*/ public class User {
private int id;
private String userName;
private String passWord;
private String hobby;
private String gender;
private String position;
private String resume;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
public String getPassWord() { return passWord; }
public void setPassWord(String passWord) { this.passWord = passWord; }
public String getHobby() { return hobby; }
public void setHobby(String hobby) { this.hobby = hobby; }
public String getGender() { return gender; }
public void setGender(String gender) { this.gender = gender; }
public String getPosition() { return position; }
public void setPosition(String position) { this.position = position; }
public String getResume() { return resume; }
public void setResume(String resume) { this.resume = resume; }
}
二:這樣咱們創建好了實體對象以後,如今咱們來創建UserDAO接口和DAOFactory兩個類以下:
package dao;
import vo.User;
public interface UserDAO { //創建一個接口 void save(User user);
}
package factory;
import impl.UserDaoImpl; import dao.UserDAO;
public class DAOFactory { public static UserDAO getUserDAOInstance(){ return new UserDaoImpl(); }
}
三:如今咱們來創建ConnectionUtil類,這裏我才用咱們最經常使用的一種方法來鏈接數據庫Database即:
package db;
import java.sql.Connection; import java.sql.DriverManager; import java.util.Properties;
public class ConnectionUtil {
public Connection openConnection() { String driver = ""; String url = ""; String user = ""; String password = ""; Properties prop = new Properties();
//實例化一個配置文件的對象
Connection conn = null;
try {
// 加載屬性文件
prop.load(this.getClass().getClassLoader().getResourceAsStream( "DBConfig.properties"));
driver = prop.getProperty("driver");
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("password");
// Class.forName加載驅動
Class.forName(driver);
// DriverManager得到鏈接
conn = DriverManager.getConnection(url, user, password); return conn;
} catch (Exception e) { e.printStackTrace(); }
return null; } }
四:是實現Java程序與數據庫的連接層創建一個UserDaoImpl類:
package impl;
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException;
import vo.User; import dao.UserDAO; import db.ConnectionUtil;
public class UserDaoImpl implements UserDAO{ //實現接口UerDAO
public void save(User user) {
//實現接口類中的方法
ConnectionUtil cu=new ConnectionUtil();
//初始化鏈接的數據庫 Connection conn=cu.openConnection();
String sql = "insert into User(username,password,hobby,gender,position,resume) values(?,?,?,?,?,?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getPassWord());
pstmt.setString(3, user.getHobby());
pstmt.setString(4, user.getGender());
pstmt.setString(5, user.getPosition());
pstmt.setString(6, user.getResume());
pstmt.executeUpdate(); }
catch (SQLException e) { e.printStackTrace(); }
finally{ try { conn.close();//關閉鏈接數據庫 } catch (SQLException e) { e.printStackTrace();
} } } }
五:是經過網站往數據庫中加載數據,以下:
package com.bx.Servletform;
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import dao.UserDAO;
import factory.DAOFactory;
import vo.User;
public class RegisterServlet extends HttpServlet {
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
String username=request.getParameter("username");
String password=request.getParameter("password");
String[] hobby=request.getParameterValues("hobby");
String gender=request.getParameter("gender");
String resume=request.getParameter("resume");
String position=request.getParameter("position");
User user=new User();
user.setUserName(username);
user.setGender(gender);
user.setPassWord(password);
user.setPosition(position);
user.setResume(resume);
String hobbyStr="";
if(hobby!=null&&hobby.length>0){
for(int i=0;i<hobby.length;i++){ hobbyStr=hobby[i]+":"; } }
user.setHobby(hobbyStr);
UserDAO udao=DAOFactory.getUserDAOInstance();
udao.save(user);
PrintWriter out=response.getWriter();
out.println("Hello World Servlet james");
System.out.println("username |"+username);
System.out.println("password |"+password);
System.out.println("gender |"+gender);
System.out.println("resume |"+resume);
System.out.println("position |"+position);
if(hobby!=null&& hobby.length>0){
for(int i=0;i<hobby.length;i++){
System.out.println(hobby[i]); }
}
//實現頁面的跳轉
if(username!=null && username.equals("james")){
request.getRequestDispatcher("/success.html").forward(request, response);
}else{
request.getRequestDispatcher("/failure.jsp").forward(request, response);
} }
如今咱們來配置一下register,jsp吧,以下:
<form action="/Servlet_Form_Project/form" method="post">
<!--form 中有兩個最爲重要的屬性--> 用戶名稱:<input type="text" name="username"><br>
用戶密碼:<input type="password" name="password"><br>
用戶愛好:<input type="checkbox" name="hobby" value="1">游泳
<input type="checkbox" name="hobby" value="2">足球<br>
用戶性別:<input type="radio" name="gender" value="male">男
<input type="radio" name="gender" value="female">女
<input type="radio" name="gender" value="secret">保密<br>
用戶職位:<select name="position">
<option value="CEO">CEO</option>
<option value="CFO">CFO</option>
<option value="CTO">CTO</option>
</select><br>
用戶簡歷:<textarea rows="5" cols="20" name="resume"></textarea><br/>
<input type="submit" value="註冊">
</form>
如今咱們來配置一下WEB.xml以下所示:
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.bx.Servletform.RegisterServlet</servlet-class>
<!-- this is servlet --> </servlet> <servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/form</url-pattern>
</servlet-mapping> 在此咱們已經完成了Java Web與數據庫的鏈接:至此咱們能夠開始comcat,在瀏覽器中輸入鏈接的地址即:http://localhost:8080/Servlet_Form_Project/register.jsp 獲得頁面爲