JDBC - 开发实例 - MVC模式
1. 在web.xml中配置连接数据库的信息
web.xml:
2. 数据层-创建实体类Student.java
Student.java:
package gh.test.entity;
public class Student{
private int id;
private String name;
private int gender;
private int age;
private String address;
private String tel;
public void setId(int id){ //学号
this.id = id;
}
public int getId(){
return id;
}
public void setName(String name){ //姓名
this.name = name;
}
public String getName(){
return name;
}
public void setGender(int gender){ //性别
this.gender = gender;
}
public int getGender(){
retuen gender;
}
public void setAge(int age){ //年龄
this.age = age;
}
public int getAge(){
return age;
}
public void setAddress(String address){ //地址
this.address = address;
}
public String getAddress(){
return address;
}
public void setTel(String tel){
return tel;
}
}
3. 数据访问层-创建数据库操作类 DB.java & 学生DAO类 StudentDao.java
DB.java:
package gh.test.dao;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DB{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
public Connection getConn
(String server,String db,String user,String password)throws ClassNotFoundException,InstantiationException,IllegalAccessException,SQLException{ //获取对象
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://"+server+":3306/"+db+"?user="+user+"&password="+password+"&characterEncoding=utf-8";
Class.forName(driver); //加载驱动
Connection conn = DriverManager.getConnection(url); //创建Connection对象
return conn;
}
public ResultSet executeQuery(String sql,String[] params){ //执行查询操作
try{
ps = conn.prepareStatement(sql);
if(params != null){
for(int i=0;i ps.setString(i+1,params[i]); } } rs = ps.executeQuery(); } catch(SQLException ex){ ex.printStackTrace(); } return rs; } public int executeUpdate(String sql,String[] params){ //执行更新操作 int n = 0; try{ ps = conn.prepareStatement(sql); if(params != null){ for(int i=0;i ps.setString(i+1,params[i]); } n = ps.executeUpdate(); } }catch(SQLException ex){ ex.printStackTrace(); } return n; } public void closeAll(){ if(rs != null){ try{ rs.close(); }catch(SQLException ex){ ex.printStackTrace(); } } if(ps != null){ try{ ps.close(); }catch(SQLException ex){ ex.printStackTrace(); } } if(conn != null){ try{ conn.close(); }catch(SQLException ex){ ex.printStackTrace(); } } } } StudentDao.java: package gh.test.dao; import gh.test.entity.Student; public class StudentDao extends DB{ public int addStudent(Student student){ String name = student.getName(); String gender = student.getGender()+""; String age = student.getAge()+""; String address = student.getAddress(); String tel = student.getTel(); String[] params = new String[]{name,gender,age,address,tel}; String sql = "insert into studentInfo values(?,?,?,?,?)"; int result = super.executeUpdate(sql,params); return result; } } 4. 视图显示层&逻辑处理-addStudent.html & AddStudentServlet.java addStudent.html:
function check(){
if(document.form1.name.value == ""){
alert("请输入姓名!");
document.form1.name.focus();
return false;
}
if(document.form1.age.value == ""){
alert("请输入年龄!");
document.form1.age.focus();
return false;
}
if(document.form1.address.value == ""){
alert("请输入地址!");
document.form1.address.focus();
return false;
}
if(document.form1.tel.value == ""){
alert("请输入电话号码!");
document.form1.tel.focus();
return false;
}
return true;
}
AddStudentServlet.java:
package gh.test.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletContent;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import gh.test.entity.Student;
import gh.test.dao.StudentDao;
public class AddStudentServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
public AddStudentServlet(){
super();
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
doPost(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
int gender = Integer.parseInt(request.getParameter("gender"));
int age = Integer.parseInt(request.getParameter("age"));
String address = request.getParameter("address");
String tel = request.getParameter("tel");
Student student = new Student();
student.setName(name);
student.setGender(gender);
student.setAge(age);
student.setAddress(address);
student.setTel(tel);
ServletContext ctx = this.getServletContext();
String server = ctx.getInitParameter("server");
String db = ctx.getInitParameter("db");
String user = ctx.getInitParameter("user");
String password = ctx.getInitParameter("password");
StudentDao studentDao = new StudentDao();
try{
studentDao.getConn(server,db,user,password);
if(studentDao.addStudent(student) == 1){
out.print("添加新同学信息成功!
");
}
else{
out.print("添加失败!
");
}
out.print("返回");
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}catch(Exception ex){
ex.printStackTrace();
}
}
}