JDBC-development Example-MVC pattern
1. Configure the connection database information in Web. xml
Web. xml:
<context-param>
<param-name>server</param-name>//Host Name
<param-value>localhost</param-value>
</context-param>
<context-param>
<param-name>db</param-name>//Database
<param-value>test</param-value>
</context-param>
<context-param>
<param-name>user</param-name>//user name
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>password</param-name>//password
<param-value>1234</param-value>
</context-param>
2. Data layer-Create entity class 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) {//study number
This.id = ID;
}
public int getId () {
return ID;
}
public void SetName (String name) {//Name
THIS.name = name;
}
Public String GetName () {
return name;
}
public void Setgender (int gender) {//Gender
This.gender = gender;
}
public int Getgender () {
Retuen gender;
}
public void Setage (int ages) {//Age
This.age = age;
}
public int getage () {
return age;
}
public void setaddress (String address) {//Addresses
this.address = address;
}
Public String getaddress () {
return address;
}
public void Settel (String tel) {
return Tel;
}
}
3. Data Access Layer-create Database operations Class Db.java & Students DAO class 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{//Get Object
String Driver = "Com.mysql.jdbc.Driver";
String url = "jdbc:mysql://" +server+ ": 3306/" +db+ "? user=" +user+ "&password=" +password+ "&characterencoding= Utf-8 ";
Class.forName (driver); Load Driver
Connection conn = drivermanager.getconnection (URL); Create a Connection object
Return conn;
}
Public ResultSet executeQuery (String sql,string[] params) {//Execute query operation
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) {//Perform update operation
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. View display layer & logic processing-addstudent.html & Addstudentservlet.java
Addstudent.html:
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8" >
<title> Add student Information </title>
<body>
<form name= "Form1" action= "Addstudentservlet" method= "POST" >
<table>
<tr>
<th colspan= "2" > Student information </th>
</tr>
<tr>
<td> name:</td>
<td><input type= "text" name= "name"/></td>
</tr>
<tr>
<td> Gender:</td>
<td>
<input type= "Radio" name= "Gender" value= "0" check= "checked"/> Male
<input type= "Radio" name= "Gender" value= "1"/> Female
</td>
</tr>
<tr>
<td> Age:</td>
<td>
<select name= "Age" >
<option value= "" select= "selected" >18</option>
<option value= ">19</option>"
<option value= ">20</option>"
<option value= ">21</option>"
<option value= ">22</option>"
</select>
</td>
</tr>
<tr>
<td> Address:</td>
<td><input type= "text" name= "Address"/></td>
</tr>
<tr>
<td> Telephone:</td>
<td><input type= "Text" name= "Tel"/></td>
</tr>
<tr>
<TD colspan= "2" align= "Center" >
<input type= "Submit" Name= "TJ" value= "Submit" onclick= "check ();" />
<input type= "reset" name= "CZ" value= "reset"/>
</td>
</tr>
</table>
</form>
<script language= "JavaScript" >
function Check () {
if (Document.form1.name.value = = "") {
Alert ("Please enter your name!") ");
Document.form1.name.focus ();
return false;
}
if (Document.form1.age.value = = "") {
Alert ("Please enter your age! ");
Document.form1.age.focus ();
return false;
}
if (Document.form1.address.value = = "") {
Alert ("Please enter an address!") ");
Document.form1.address.focus ();
return false;
}
if (Document.form1.tel.value = = "") {
Alert ("Please enter your phone number!") ");
Document.form1.tel.focus ();
return false;
}
return true;
}
</script>
</body>
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 ("Add new classmate Information success!") <br> ");
}
else{
Out.print ("Add failed! <br> ");
}
Out.print ("<a href= ' addstudent.html ' > Return </a>");
}catch (ClassNotFoundException ex) {
Ex.printstacktrace ();
}catch (Exception ex) {
Ex.printstacktrace ();
}
}
}
This article is from "Time Diary" blog, please make sure to keep this source http://megxh51.blog.51cto.com/12602415/1907901
JDBC-development Example-MVC pattern