首页 > 代码库 > JDBC - 开发实例 - MVC模式
JDBC - 开发实例 - MVC模式
JDBC - 开发实例 - MVC模式
1. 在web.xml中配置连接数据库的信息
web.xml:
<context-param>
<param-name>server</param-name> //主机名
<param-value>localhost</param-value>
</context-param>
<context-param>
<param-name>db</param-name> //数据库
<param-value>test</param-value>
</context-param>
<context-param>
<param-name>user</param-name> //用户名
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>password</param-name> //密码
<param-value>1234</param-value>
</context-param>
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<params.length;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<params.length;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:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>添加学生信息</title>
</head>
<body>
<form name="form1" action="AddStudentServlet" method="post">
<table>
<tr>
<th colspan="2">学生信息</th>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>性别:</td>
<td>
<input type="radio" name="gender" value="http://www.mamicode.com/0" check="checked"/>男
<input type="radio" name="gender" value="http://www.mamicode.com/1"/>女
</td>
</tr>
<tr>
<td>年龄:</td>
<td>
<select name="age">
<option value="http://www.mamicode.com/18" select="selected">18</option>
<option value="http://www.mamicode.com/19">19</option>
<option value="http://www.mamicode.com/20">20</option>
<option value="http://www.mamicode.com/21">21</option>
<option value="http://www.mamicode.com/22">22</option>
</select>
</td>
</tr>
<tr>
<td>地址:</td>
<td><input type="text" name="address"/></td>
</tr>
<tr>
<td>电话:</td>
<td><input type="text" name="tel"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="tj" value="http://www.mamicode.com/提交" onclick="check();"/>
<input type="reset" name="cz" value="http://www.mamicode.com/重置"/>
</td>
</tr>
</table>
</form>
<script language="javascript">
function check(){
if(document.form1.name.value =http://www.mamicode.com/=""){
alert("请输入姓名!");
document.form1.name.focus();
return false;
}
if(document.form1.age.value =http://www.mamicode.com/=""){
alert("请输入年龄!");
document.form1.age.focus();
return false;
}
if(document.form1.address.value =http://www.mamicode.com/=""){
alert("请输入地址!");
document.form1.address.focus();
return false;
}
if(document.form1.tel.value =http://www.mamicode.com/=""){
alert("请输入电话号码!");
document.form1.tel.focus();
return false;
}
return true;
}
</script>
</body>
</html>
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("添加新同学信息成功!<br>");
}
else{
out.print("添加失败!<br>");
}
out.print("<a href=http://www.mamicode.com/‘addStudent.html‘>返回");
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
本文出自 “时光日记” 博客,请务必保留此出处http://megxh51.blog.51cto.com/12602415/1907901
JDBC - 开发实例 - MVC模式