这周因为要准备java和四级考试,所以没有做什么太多的东西,只是把我们之前做的javaweb项目又重新写了一下,现在就简单的说一下我做的东西.
我做的就是一个简单的用户注册和管理员页面.下面是我项目的一个简单的框架.
下面我就一个删除的操作简单的说明一下,因为其他的大概都是一样的.
1,首先数据库要连接,我这里连接的是MySQL,也就是我上面的那个utils包里的java类,就是我的连接数据库的代码.
package com.qmx.web.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtils {
static String loginName = "root";
static String loginPassword = "123456";
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection GetConnection() throws SQLException{
String url = "jdbc:mysql://localhost:3306/webdb";
return DriverManager.getConnection(url, loginName, loginPassword);
}
public static void main(String args[]) throws SQLException{
System.out.println(GetConnection());
}
}
2,接下来是要准备好操作的对象,也就是我项目里面的User类
package com.qmx.web.pro;
public class User {
private int id; //编号
private String name; //姓名
private String sex; //性别
private int age; //年龄
private String profess;//专业
private String grade; //年级
private String phone; //电话
public User() {
}
public User(String name, String sex, int age, String profess, String grade, String phone) {
this.name = name;
this.sex = sex;
this.age = age;
this.profess = profess;
this.grade = grade;
this.phone = phone;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getProfess() {
return profess;
}
public void setProfess(String profess) {
this.profess = profess;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + ", profess=" + profess
+ ", grade=" + grade + ", phone=" + phone + "]";
}
}
3,接下来就是定义我的接口以及实现接口,这个项目用到的主要知识是dao工厂模式.主要的我目前搞的也不是很清楚,但是大概会那么用.
package com.qmx.web.common;
import java.sql.SQLException;
import java.util.List;
public interface IBaseDao<K,V> {
public boolean doInsert(V v) throws SQLException;
public boolean doDelete(K id) throws SQLException;
public boolean doUpdate(V v) throws SQLException;
public V findById(K id) throws Exception;
public List<V> findAll() throws SQLException;
public List<V> findAllPaging(String column,String keyWord,int currentPage,int pageSize);
}
package com.qmx.web.dao;
import com.qmx.web.common.IBaseDao;
import com.qmx.web.pro.User;
public interface UserDao extends IBaseDao<String,User>{
}
下面这个代码仅仅是我实现的删除的代码,不是全部,主要就是UserDaoimpl类实现UserDao接口,
public class UserDaoImpl implements UserDao
@Override
public boolean doDelete(String id) throws SQLException {
conn = DBUtils.GetConnection();
String sql = "delete from user where name = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, id);
if(ps.executeUpdate() > 0){
if(ps != null)
ps.close();
if(conn != null)
conn.close();
return true;
}
return false;
}
下面是service包中的方法
5,然后是创建一个简单servlet,继承HttpServlet类,并实现doGet、doPost方法
package com.qmx.web.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.qmx.web.service.UserService;
public class DelServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
UserService userService = new UserService();
try {
if(userService.doDelete(name)){
req.getRequestDispatcher("delsuccess.jsp").forward(req, resp);
}else{
req.getRequestDispatcher("delfail.jsp").forward(req, resp);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
上面的我是使用了doGet()方法,因为我后面的jsp文件使用了a链接来操作删除方法,使用a链接默认的方法是doGet()方法.
6,然后就是编写jsp文件了
<td><a href="DelServlet?name=<%=t.getName()%>">删除</a></td>
7,之后别忘了要配置XML文件
<servlet>
<servlet-name>DelServlet</servlet-name>
<servlet-class>com.qmx.web.controller.DelServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DelServlet</servlet-name>
<url-pattern>/DelServlet</url-pattern>
</servlet-mapping>
8,之后如果没有其他的错误的话,项目就会成功