使用三层架构(数据访问层、业务逻辑层、表示层)使得编程更加清晰。
util包,用于加载数据库驱动,获得驱动管理信息。
public Connection bdHelper{
private final String DRIVER ="com.mysql.jdbc.Driver";
private final String URL = "jdbc:mysql://localhost:3306/mySQL_name";
private final String USER ="root";
private String PWD = "root";
Connection con = null;
try{
Class.forName(DRIVER);
con = DrverManager.getConnection(URL,USER,PWD);
}catch(Exception e){
e.PrintStackTrace();
}
return con;
}
public void close(ResultSet rs,PreparedStatement ps , Connection con){
if(rs!=null){rs.close();}
if(ps!=null){ps.close();}
if(con!=null){con.close();}
}
用一个entity包封装所要查询的数据库信息(com.lxchen.entity)
public class Student{
private int id;
private String name;
private int age;
private String sex;
public Student(){
}
public Student(int id, String name, int age, String sex){
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
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 int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
public String getSex(){
return sex;
}
public void setSex(int sex){
this.sex = sex;
}
数据访问层(com.lxchen.DAO),用于规定一个查询接口。
public interface StuDAO{
public List StuSelectDAO();
}
数据访问层的实现(com.lxchen.DAOImpl),用于实现查询接口,并将结果返回给业务逻辑层
public class StuDAOImpl implements StuDAO{
public List StuSelectDAO(){
dbHelper db = new dbHelper();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs =null;
List lst = new ArrayList();
try{
con = db.getConnection();
ps = con.prepareStatement("select * from table_name");
rs = ps.executeQuery();
while(rs.next()){
lst.add(new Student(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getString(4)));
}
}catch(Exception e){
e.PrintStackTrace();
}finally{
try{
db.closer(rs,ps,con);
}catch(Exception e){
e.PrintStackTrace();
}
}
return lst;
}
}
业务逻辑层(com.lxchen.Service).规定一个接口,用于从数据访问层获取查询结果。
public interface StuService{
public List StuSelectService();
}
业务逻辑实现层(com.lxchen.ServiceImpl).用于将从数据访问层获得的结果,返回给表示层。
public class StuServiceImpl implements StuService{
public List StuSelectServiceImpl(){
StuDAO sd = new StuDAOImpl();
return sd.StuSelectDAO();
}
}
建立一个Servlet容器(com.lxchen.Servlet),用于存放java代码实现在客户端页面显示的内容.
public class AjaxServlet extends HttpServlet(){
//重写service方法
protected void service(HttpServletRequest request , HttpServletResponse response)throws ServletException , IOException{
StuService ss = new StuServiceImpl();
List lst = ss.StuSelectServiceImpl();
response.setCharacterEncoding("utf-8");//防止页面显示乱码
response.setContentType("text/html"); //防止页面显示乱码
PrintWriter out = response.getWriter();//建立IO输出流,向页面输出信息
out.print("");
for(int i=0;iout.print("");
out.print(""+lst.get(i).getId()+" ");
out.print(""+lst.get(i).getName()+" ");
out.print(""+lst.get(i).getAge()+" ");
out.print(""+lst.get(i).getSex()+" ");
out.print(" ");
}
out.print("
");
}
}
在web.xml配置文件中,配置Servlet。
<servlet>
<servlet-name>StuServletservlet-name>
<servlet-class>com.lxchen.Servlet.AjaxServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>StuServletservlet-name>
<url-pattern>/AJAXServleturl-patern>
servlet-mapping>
在显示页面进行配置(showPage.jsp)
<head>
<script type="text/javascript">
var xmlhttp;
function toClick(){
if(window.XMLHttpRequest){
//判断Ajax是否支持IE7+,chrome,FireFox,etc
xmlhttp = new XMLHttpRequest();
}eler if(window.ActiveXObject){
//判断Ajax是否支持IE5,IE6低版本IE浏览器.
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if(xmlhttp!=null){
xmlhttp.onreadystatechange = state_Change;
xmlhttp.open("post","AJAXServlet",true);
xmlhttp.send();
}
}
function state_Change(){
if(xmlhttp.readyState==4&&xmlhttp.status == 200){
document.getElementById("info").innerHTML = xmlhttp.responseText;
}
}
<input type="button" value="显示" onclick="toClick()" />
<p><div id="info">INFO will be listed in herediv>p>
body>
script>
head>