考研成绩查询系统(jsp、mysql、java)
3、数据库:
数据库建表和用户:建数据库create database graduate default character set gbk;建表create table score(id_card varchar(18) primary key,name varchar(20),math float(6,2),english float(6,2),politics float(6,2),major float(6,2),total_score float(6,2))engine=MyISAM default charset=gbk;插入insert into score(id_card,name, math, english, politics, major)values('1','张三',130,70,69,120);insert into score(id_card,name, math, english, politics, major)values('2','李四',120,90,39,110);insert into score(id_card,name, math, english, politics, major)values('3','赵武',134,70,69,130);insert into score(id_card,name, math, english, politics, major)values('4','小胖',133,90,69,120);计算总分update score set total_score = math+english+politics+major;
创建用户grant all privileges on graduate.* to 'webuser'@'%'identified by 'webpass';grant all privileges on graduate.* to 'webuser'@'localhost' identified by 'webpass';flush privileges; 4、jsp源码
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>考研成绩查询系统</title>
</head>
<body>
<%
request.setCharacterEncoding("GB18030");
String idcard = request.getParameter("idcard");
String name = request.getParameter("name");
if(idcard == null) idcard = "";
else idcard = idcard.trim();
if(name == null)name = "";
else name = name.trim();
request.setAttribute("idcard", idcard);
request.setAttribute("name", name);
%>
<h2>考研成绩查询系统</h2>
<form action="score.jsp" method="post">
<p>身份证:<input name="idcard" type="text" value="${idcard}"></p>
<p>姓 名:<input name="name" type="text" value="${name}"></p>
<p style="padding-left:150px;">
<input type="submit" name="submit" value="查询">
</p>
</form>
<%
if(! ("".equals(idcard) && "".equals(name))){
Connection conn = null;
Statement state = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/graduate?useUnicode=true&characterEncoding=gbk";
conn = DriverManager.getConnection(url, "webuser", "webpass");
state = conn.createStatement();
String sql = "select * from score where id_card='"+idcard+"'";
System.out.println("调试打印语句sql:" + sql);
rs = state.executeQuery(sql);
while(rs.next()){
String nameDB = rs.getString("name");
if(name.equals(nameDB)){
float math = rs.getFloat("math");
float english = rs.getFloat("english");
float politics = rs.getFloat("politics");
float major = rs.getFloat("major");
float total = rs.getFloat("total_score");
%>
<table width="580" border="0" cellspacing="1">
<tr align="center">
<th>身份证号码</th>
<th>姓名</th>
<th>数学</th>
<th>英语</th>
<th>政治</th>
<th>专业综合</th>
<th>总分</th>
</tr>
<tr align="center">
<td><%=idcard%></td>
<td><%=name%></td>
<td><%= math%></td>
<td><%= english%></td>
<td><%= politics%></td>
<td><%= major%></td>
<td><%= total%></td>
</tr>
</table>
<%
break;//跳出while循环
}else{
out.println("<p>身份证号与姓名不匹配! 身份证号:"+idcard+",姓名:"+name+"</p>");
}
rs.close();
state.close();
conn.close();
}
}catch(SQLException e){
e.printStackTrace();
}finally{
try{rs.close();}catch(Exception e1){
System.out.println("结果集ResultSet没有关闭"+"\n"+"异常信息为:");
}
try{state.close();}catch(Exception e2){
System.out.println("Statement没有关闭"+"\n"+"异常信息为:");
}
try{conn.close();}catch(Exception e3){
System.out.println("Connection没有关闭"+"\n"+"异常信息为:");
};
}
}
%>
</body>
</html>
更多相关文章
- [置顶] Android数据库框架GreenDao封装使用,易理解、易扩
- Android原生Contacts——界面和数据库
- [Android]如何导入已有的外部数据库
- Java操作数据库之jdbc【原生方式】
- Android复习练习十二(自定义ContentProvider实现其他应用操作本
- 如何从android的联系人姓名中获取联系人的号码?
- 实现基于注解(Annotation)的数据库框架(一)反射的基本了解
- 无法从Android中的Asset文件夹复制数据库
- Android之SQLite数据库篇