简单数据库数据及路径:
流程思路:
查询页 user.jsp
实体类 com.bingo.entity包下:User.java
实现类 com.bingo.DAO下,BaseDao.java UserDao.java
代码如下:
User.java
package com.bingo.entity;
/*
* 实体类
*/
public class User {
private int id;
private String name;
private String password;
public User() {
super();
}
public User(int id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
BaseDao.java
package com.bingo.DAO;
import java.sql.*;
public class BaseDAO {
public static Connection
getConnection(String host,String datebase,String user,String password) throws SQLException {
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//建立数据连接
Connection conn = null;
try {
String url="jdbc:mysql://"+host+":3306/"+datebase+"?useUnicode=true&characterEncoding=UTF-8";
conn=DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
return conn;
}
//关闭连接方法
public static void closeAll(Statement st,ResultSet rs,Connection conn){
try {
rs.close();
st.close();
conn.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
UerDao.java
package com.bingo.DAO;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.bingo.entity.User;
import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;
public class UserDao extends BaseDAO {
public static List<User> getAll() throws SQLException{
Connection conn = getConnection("127.0.0.1", "test", "root", "");
List<User> list = new ArrayList<User>();
Statement st = null;
String sql=null;
ResultSet rs=null;
try {
st = conn.createStatement();
sql = "select * from user2";
rs = st.executeQuery(sql);
while(rs.next()){
User user = new User(rs.getInt(1),
rs.getString(2),rs.getString(3));
list.add(user);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
}finally {
closeAll(st, rs, conn);
}
return list;
}
}
user.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="com.bingo.entity.User"%>
<%@page import="com.bingo.DAO.UserDao"%>
<%@page import="java.util.List" %>
<%@page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
List<User> list = UserDao.getAll();
for(User user:list){
out.print(user.getId());
out.print(" "+user.getName());
out.print(" "+user.getPassword()+"<br>");
}
%>
</body>
</html>