1、把数据库建好
CREATE TABLE `User` (
`id` int(11) NOT NULL,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
)
2、插入一个数据
insert into User values(1,"Martin","12345","zjut")
3、建立一个Dynamic Web Project,目录如下:
4、showinfo.jsp,这里需要注意的是导入类:import="com.entity.User"
<%@ page language="java" contentType="text/htmlcharset=UTF-8"
pageEncoding="UTF-8" import="com.entity.User"%>
<!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/htmlcharset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
User user = (User)request.getAttribute("user")
%>
(此处空一行)
id:<%=user.getId() %>
username:<%=user.getUsername() %>
password:<%=user.getPassword() %>
address:<%=user.getAddress() %>
(此处空一行)
<!--
也可以用el表达式,但是同样需要servlet先将值存放在request中,但是用el表达式的话需要在上面加上:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
如下,获取id这样写就可以
${user.getId()}
-->
</body>
</html>
5、结果如下:
<%@ page language="java" import="java.sql.*" pageEncoding="utf-8" errorPage="error.jsp"%><html>
<head>
<script type="text/javascript">
<!--
function modifyStu(){
var isMod=confirm("确定要修改该学生信息吗?")
if(!isMod)
return false
}
function deleteStu(){
var isDel=confirm("确定要删除该学生的信息吗?")
if(!isDel)
return false
}
-->
</script>
<title>list.jsp</title>
</head>
<body>
<table border="1" bgcolor="#ffffff" align="center" cellspacing="1" cellpadding="1">
<caption>学籍管理系统</caption>
<tr>
<td align="center" width=16%>学号</td>
<td align="center" width=16%>姓名</td>
<td align="center" width=8%>性别</td>
<td align="center" width=8%>年龄</td>
<td align="center" width=16%>籍贯</td>
<td align="center" width=12%>院系</td>
<td align="center" width=12%>更改</td>
<td align="center" width=12%>删除</td>
</tr>
<%
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
String url="jdbc:microsoft:sqlserver://localhost:1433DatabaseName=university"
Connection con=DriverManager.getConnection(url,"admin111","111111")
Statement stmt=con.createStatement()
String sql="select * from student"
ResultSet rs=stmt.executeQuery(sql)
while(rs.next()){
%>
<tr>
<td width=16% align="center"><%=rs.getObject(1) %></td>
<td width=16% align="center"><%=rs.getObject(2) %></td>
<td width=8% align="center"><%=rs.getObject(3) %></td>
<td width=8% align="center"><%=rs.getObject(4) %></td>
<td width=16% align="center"><%=rs.getObject(5) %></td>
<td width=12% align="center"><%=rs.getObject(6) %></td>
<td width=12% algin="center" onclick="return modifyStu()"><a href="change.jsp?xuehao=<%=rs.getObject(1) %>">修改</a></td>
<td width=12% algin="center" onclick="return deleteStu()"><a href="del.jsp?xuehao=<%=rs.getObject(1) %>">删除</a></td>
</tr>
<%
}
rs.close()
stmt.close()
con.close()
}catch(Exception e){
e.printStackTrace()
}
%>
</table>
<div align="center"><a href="insert.jsp">添加新记录</a></div>
</body>
</html>
楼主可以看一下这个简单的例子...
楼主就会明白的..
如果还是看不懂或是有什么不明白的可以来百度HI我
用jsp显示数据库中的数据的方法:1、通过jdbc建立数据库连接:
Connection connection = DriverManager.getConnection(
"jdbc:odbc:data", "Steve", "password")
2、创建查询的statement:
Statement statement = connection.createStatement()
3、执行查询:
ResultSet resultset =
statement.executeQuery("select * from tableName")
4、循环输出获取到的数据:
while(resultset.next()){
。。。。。。
}
5、综合1-4的完整代码如下:
<%@ page import="java.sql.*" %>
<% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")%>
<HTML>
<HEAD>
<TITLE>The tableName Database Table </TITLE>
</HEAD>
<BODY>
<H1>The tableName Database Table </H1>
<%
Connection connection = DriverManager.getConnection(
"jdbc:odbc:data", "Steve", "password")
Statement statement = connection.createStatement()
ResultSet resultset =
statement.executeQuery("select * from tableName")
%>
<TABLE BORDER="1">
<TR>
<TH>ID</TH>
<TH>Name</TH>
<TH>City</TH>
<TH>State</TH>
<TH>Country</TH>
</TR>
<% while(resultset.next()){ %>
<TR>
<TD><%= resultset.getString(1) %></td>
<TD><%= resultset.getString(2) %></TD>
<TD><%= resultset.getString(3) %></TD>
<TD><%= resultset.getString(4) %></TD>
<TD><%= resultset.getString(5) %></TD>
</TR>
<% } %>
</TABLE>
</BODY>
</HTML>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)