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、结果如下:
sm = con.prepareStatement("select from iron where id='"+传的id+"'")rs = sm .executeQuery()
然后将表中的信息存放到一个类中
while(rs.next()){
id=rs.getString("id")
name=rs.getString("name")
...
//可以在servlet中打印信息验证
}
//也可以直接在jsp页面打印出来.
是输出到下拉菜单吗?应该这样:<form id="test" method="post" >
<select id="code" name="plugin">
<option value=>请选择</option>
<option value="1">钢材</option>
<option value="2">铸铁</option>
<%
String num = request.getParameter("plugin")
int id= Integer.parseInt(num)
String st="select * from steel"
String ti="select * from titanium"
String Qq
if(id==1)
{ Qq=st}
else
{ Qq=ti}
Connection con = null
Statement sm = null
ResultSet rs = null
try
{
Class.forName("org.postgresql.Driver").newInstance()
String url="jdbc:postgresql://localhost/Anti-fatigue Design"
String user="postgres"
String password="123456"
con = DriverManager.getConnection(url,user,password)
sm = con.createStatement()
rs = sm.executeQuery(Qq)
while(rs.next())
{
out.print("<option value=''>"+rs.getString(2)+"</option>")
}
}
catch(Exception e)
{
e.printStackTrace()
}
%></select>
<input type="submit" value="提交">
</form>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)