基于数据库的Java Web
现今所有的 Web都是基于数据库的,可见数据库在Web中的开发时如此的重要。然而基本上所有的网站都是要求客户登陆的时候都必须事先注册账号,这样才能进入网页,访问其中的页面,资源。。开发基于数据库的Web程序时,涉及到多个用户同时请求与数据库建立"连接"的问题,但多用户并发处理目前还是一个难点。该文在对传统数据连接模式的分析基础上,采用了一种基于对象的数据连接池的设计方案。通过连接对象放置预先建立的若干"连接",根据一定的策略管理这些"连接",防止"连接"过多内存开销过大,及连接排队过长问题。并用JAVA实现了该方案,在实际应用中提供了一个高效的连接管理策略,提高了Web数据库应用系统的性能。
一:在此我通过一个实际的例子谈谈我最近最Web得心得,首先我们建立一个javaWeb项目在Myeclipse下。首先建立好数据库连接,即建立一个Vo对象User类,如下:
package vo;
/*
用户名称:<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>
<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
得到页面为:
这时候我们可以查看数据库中是否在如了我们输入的数据即:
posted on 2010-12-09 20:55
龍ぜ殘剑 阅读(4392)
评论(2) 编辑 收藏