jsp+mysql添加查询代码,并把查询结果返回当前页面,可以按学号,姓名或成绩查询
表的结构:createtablescore(numvarchar(12)primarykey,namevarchar(20),scoredecimal(5,2));JSP...
表的结构:
create table score
(num varchar(12) primary key,
name varchar(20),
score decimal(5,2)
);
JSP代码:(在里面插入查询代码)
<%@ page contentType="text/html;charset=UTF-8" 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>JSP Page</title>
</head>
<body style="text-align:center">
<table width="400" border="1" align="center">
<caption>学生成绩管理</caption>
<tr><td><div align="center"><input type="button" value="新增" onclick="location.href='add.jsp?'"></div></td></tr>
<tr>
<td><div align="center">学号</div></td>
<td><div align="center">姓名 </div></td>
<td><div align="center">成绩</div></td>
<td><div align="center">修改</div></td>
<td><div align="center">删除</div></td>
</tr>
<%
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student", "ting", "111");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from score ");
String num,name;
float score;
if (rs!=null)
{
while (rs.next()){
num=rs.getString("num");
name=rs.getString("name");
score=rs.getFloat("score");
%>
<tr>
<td><div align="center"><%=num%></div></td>
<td><div align="center"><%=name%></div></td>
<td><div align="center"><%=score%></div></td>
<td><div align="center"><input type="button" value="修改资料" onclick="location.href='edit.jsp?num=<%=num%>'"></div></td>
<td><div align="center"><input type="button" value="删除资料" onclick="location.href='del.jsp?num=<%=num%>'"></div></td>
</tr>
<%
}
rs.close();
}
st.close();
con.close();
}
catch(Exception e)
{
throw e;
}
%>
</table>
</body>
</html> 展开
create table score
(num varchar(12) primary key,
name varchar(20),
score decimal(5,2)
);
JSP代码:(在里面插入查询代码)
<%@ page contentType="text/html;charset=UTF-8" 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>JSP Page</title>
</head>
<body style="text-align:center">
<table width="400" border="1" align="center">
<caption>学生成绩管理</caption>
<tr><td><div align="center"><input type="button" value="新增" onclick="location.href='add.jsp?'"></div></td></tr>
<tr>
<td><div align="center">学号</div></td>
<td><div align="center">姓名 </div></td>
<td><div align="center">成绩</div></td>
<td><div align="center">修改</div></td>
<td><div align="center">删除</div></td>
</tr>
<%
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student", "ting", "111");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from score ");
String num,name;
float score;
if (rs!=null)
{
while (rs.next()){
num=rs.getString("num");
name=rs.getString("name");
score=rs.getFloat("score");
%>
<tr>
<td><div align="center"><%=num%></div></td>
<td><div align="center"><%=name%></div></td>
<td><div align="center"><%=score%></div></td>
<td><div align="center"><input type="button" value="修改资料" onclick="location.href='edit.jsp?num=<%=num%>'"></div></td>
<td><div align="center"><input type="button" value="删除资料" onclick="location.href='del.jsp?num=<%=num%>'"></div></td>
</tr>
<%
}
rs.close();
}
st.close();
con.close();
}
catch(Exception e)
{
throw e;
}
%>
</table>
</body>
</html> 展开
1个回答
展开全部
题目是mysql,
但是代码中是sqlserver数据库:
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student", "ting", "111");
这个代码抛什么错误么????
[补充回答]
就按照下面的修改你的代码,下面的都是我手写的,可能哪儿会拼错,到时候你在调试下小错误,思路就是如此: 页面加一个form,加一个查询的输入,并提交给自身页面.
<body style="text-align:center">
<table width="400" border="1" align="center">
<caption>学生成绩管理</caption>
<tr><td><div align="center">
<input type="button" value="新增" onclick="location.href='add.jsp?'"> <br>
<form name="form1" method="post" action="query.jsp"> <!-- 这里Action就写本页jsp的地址 -->
查询条件<br>
<select name="keytype" id="keytype">
<option value="1">按学号</option>
<option value="2">按姓名</option>
<option value="3">按得分</option>
</select>
<input type="text" id="keyname" name="keyname" value="">
<input type="submit" value="查询"><br>
</form>
</div></td></tr>
<tr>
<td><div align="center">学号</div></td>
<td><div align="center">姓名 </div></td>
<td><div align="center">成绩</div></td>
<td><div align="center">修改</div></td>
<td><div align="center">删除</div></td>
</tr>
<%
//获取提交的查询条件
String keytype = request.getParameter("keytype");
String keyname = request.getParameter("keyname");
//拼接查询条件
String sqlc="";
if ("".equals(keyname) || keyname==null)
sqlc="";
else{
if ("1".equals(keytype))
sqlc=" and num='"+keyname+"'";
else if ("2".equals(keytype))
sqlc=" and name='"+keyname+"'";
else if ("3".equals(keytype))
sqlc=" and score="+keyname;
}
//out.println(sqlc); //这里可以打出来sqlc看看是否正确
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student", "ting", "111");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from score where 1=1 "+sqlc); //这里跟上条件
String num,name;
float score;
if (rs!=null)
{
while (rs.next()){
num=rs.getString("num");
name=rs.getString("name");
score=rs.getFloat("score");
%>
......
但是代码中是sqlserver数据库:
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student", "ting", "111");
这个代码抛什么错误么????
[补充回答]
就按照下面的修改你的代码,下面的都是我手写的,可能哪儿会拼错,到时候你在调试下小错误,思路就是如此: 页面加一个form,加一个查询的输入,并提交给自身页面.
<body style="text-align:center">
<table width="400" border="1" align="center">
<caption>学生成绩管理</caption>
<tr><td><div align="center">
<input type="button" value="新增" onclick="location.href='add.jsp?'"> <br>
<form name="form1" method="post" action="query.jsp"> <!-- 这里Action就写本页jsp的地址 -->
查询条件<br>
<select name="keytype" id="keytype">
<option value="1">按学号</option>
<option value="2">按姓名</option>
<option value="3">按得分</option>
</select>
<input type="text" id="keyname" name="keyname" value="">
<input type="submit" value="查询"><br>
</form>
</div></td></tr>
<tr>
<td><div align="center">学号</div></td>
<td><div align="center">姓名 </div></td>
<td><div align="center">成绩</div></td>
<td><div align="center">修改</div></td>
<td><div align="center">删除</div></td>
</tr>
<%
//获取提交的查询条件
String keytype = request.getParameter("keytype");
String keyname = request.getParameter("keyname");
//拼接查询条件
String sqlc="";
if ("".equals(keyname) || keyname==null)
sqlc="";
else{
if ("1".equals(keytype))
sqlc=" and num='"+keyname+"'";
else if ("2".equals(keytype))
sqlc=" and name='"+keyname+"'";
else if ("3".equals(keytype))
sqlc=" and score="+keyname;
}
//out.println(sqlc); //这里可以打出来sqlc看看是否正确
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student", "ting", "111");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from score where 1=1 "+sqlc); //这里跟上条件
String num,name;
float score;
if (rs!=null)
{
while (rs.next()){
num=rs.getString("num");
name=rs.getString("name");
score=rs.getFloat("score");
%>
......
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询