Jdbcutil类中代码:
public class JdbcUtils extends HttpServlet {
private static final long serialVersionUID = 1L;
private static String url;
private static String username;
private static String password;
//让驱动只加载一次
static{
try {
Class.forName(“com.mysql.jdbc.Driver”);
System.out.println(“驱动加载成功”);
} catch (ClassNotFoundException e) {
System.out.println(“驱动加载失败”);
e.printStackTrace();
}
}
public JdbcUtils() {
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
//init方法中的内容会在启动这个servlet时就执行,因为在配置文件中添加了1
@Override
public void init(ServletConfig config) throws ServletException {
//从配置文件中取得传给getConnection()的三个参数,
url=config.getInitParameter(“url”);
username=config.getInitParameter(“username”);
password=config.getInitParameter(“password”);
//System.out.println(getConn());
System.out.println(“我诞生了,哈哈哈哈哈”);
}
public static Connection getConn(){
Connection conn=null;
try {
conn=DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn,Statement stmt,ResultSet set){
if(set!=null){
try {
set.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
配置文件web.xml部分代码:
JdbcUtil
/jdbc
JdbcUtil
com.xmt.utils.JdbcUtils
url
jdbc:mysql://localhost:3306/mytest
username
root
password
ROOT
1
StudentServlet
/stuServlet
StudentServlet
com.xmt.contral.StudentServlet
studentDao类中代码:
public class StudentDao {
public List getStudentList(String sql){
Connection conn=JdbcUtils.getConn();
List list=new ArrayList();
Statement stmt=null;
ResultSet set=null;
try {
//得到操作数据库的对象stmt
stmt=conn.createStatement();
set=stmt.executeQuery(sql);
while(set.next()){
Student student=new Student();
student.setSname(set.getString("Sname"));
student.setSage(set.getInt("Sage"));
student.setSsex(set.getString("Ssex"));
student.setSphone(set.getString("Sphone"));
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
JdbcUtils.close(conn, stmt, set);
}
return list;
}
}
StudentService 中代码:
public class StudentService {
public List StudentList(){
String sql=”select * from student”;
StudentDao sd=new StudentDao();
List list=sd.getStudentList(sql);
return list;
}
}
StudentServlet中代码:
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public StudentServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
StudentService stuService=new StudentService();
List list=stuService.StudentList();
//将集合存进request中
request.setAttribute("StuList", list);
//实现跳转,并且可以携带数据
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
以上代码基本可以实现从数据库中读取所有数据了,它完成了servlet与数据库间的交互,接下来只要把获取到的数据设置到页面上对应的位置即可。
在index.jsp文件中写Java代码要讲Java代码放在<% java%>中间。
如:<%
for(int i=0;i