2、复制完毕,找到我们的Java项目,最好新建一个lib文件夹专门存放我们的jar包,右键点击选择【粘贴】
3、找到我们引入的mysql驱动程序,右键点击选择【Build path】,然后选择【Add to Build path】
4、导入完成,在上方会出现Referenced libraries目录,查看其下是否有引入的mysql的jar包,如果有表明引入成功。
5、在WebRoot新建一个名为DBtest.jsp页面,输入代码:
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<jsp:directive.page import="java.sql.Date"/>
<jsp:directive.page import="java.sql.Timestamp"/>
<jsp:directive.page import="java.sql.SQLException"/>
<%
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try{
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Class.forName("com.mysql.jdbc.Driver");
//zhu ce qu dong
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/databaseweb","root","123");
//huo qu statment
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from tb_person");
%>
<form action="operatePerson.jsp" method=get>
<table bgcolor="#CCCCCC" cellspacing=1 cellpadding=5 width=100%>
<tr bgcolor="#DDDDDD">
<th></th>
<th>ID</th>
<th>姓名</th>
<th>英文名</th>
<th>性别</th>
<th>年龄</th>
<th>生日</th>
<th>备注</th>
<!-- <th>记录创建时间</th> -->
<th>操作</th>
</tr>
<%
while(rs.next()){
int id=rs.getInt("id");
int age=rs.getInt("age");
String name=rs.getString("name");
String englishName=rs.getString("english_name");
String sex=rs.getString("sex");
String description=rs.getString("description");
Date birthday=rs.getDate("birthday");
//Timestamp createTime=rs.getDate("create_time");
out.println("<tr bgcolor=#FFFFFF>");
out.println("<td><input type=checkbox name=id value="+id+"></td>");
out.println("<td>"+id+"</id>");
out.println("<td>"+name+"</id>");
out.println("<td>"+englishName+"</id>");
out.println("<td>"+sex+"</id>");
out.println("<td>"+age+"</id>");
out.println("<td>"+birthday+"</id>");
out.println("<td>"+description+"</id>");
//out.println("<td>"+createTime+"</id>");
out.println("<td>");
out.println("<a href='operatePerson.jsp?action=del&id=>"+id+"'οnclick='return confirm(\"确定删除该记录?\")'>删除</a>");
out.println("<a href='operatePerson.jsp?action=edit&id="+id+"'>修改</a>");
out.println("</td>");
out.println(" </tr>");
}
%>
</table>
<table align=left>
<tr>
<td>
<input type='hiddden' value='del' name='action'>
<a href='#' οnclick="var array=document.getElementsByName('id');for(var i=0;
i<array.length;i++){array[i].checked=true;}">全选</a>
<a href='#' οnclick="var array=document.getElementsByName('id');for(var i=0;
i<array.length;i++){array[i].checked=false;}">取消全选</a>
<input type='submit' οnclick="return confirm('即将删除所有选择的记录,是否删除?');" value='删除'>
</td>
</tr>
</table>
</form>
<%
}catch(SQLException e){
out.println("发生异常:"+e.getMessage());
e.printStackTrace();
}finally{
if (rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}
%>
将DBtest.jsp加到WebRoot目录下
5、代开浏览器输入http://127.0.0.1:8080/thirdWeb/DBtest.jsp