工具:MySQL Eclipse
MySQL:score数据库 Score表
Eclipse:
1.queryscore.jsp(表单学号填写)
2.result.jsp(查询结果展示)
3.DBmanger(数据库连接)
4.UserDAO(操作数据库方法)
5.User(用户成绩类)
queryscore.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<h1 align="center">成绩查询系统</h1>
<hr size=4>
<div align="center" >
<form action="result.jsp" method="post">
学号:<input type="text" name="no" align="center" size="50">
<input type="submit" value="查询">
</form>
</div>
</body>
result.jsp
<%@ page language="java" contentType="text/html; charset=GBK" import="aaa.*"
pageEncoding="GBK"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
<style>
table{
border-1px;
border-collapse:collapse;
border-style:solid;
border-color:grey;
}
</style>
</head>
<body>
<%String no=request.getParameter("no");%>
<jsp:useBean id="user" scope="page" class="aaa.User"></jsp:useBean>
<jsp:setProperty property="no" name="user" value="<%=no%>"/>
<%user.queryUser(); %>
<%if(user.getname()!=null)
{
%>
<div align="center">
<table>
<tr>
<td>学号</td><td>姓名</td><td>数据结构</td><td>数据库</td><td>java</td><td>web</td>
</tr>
<tr>
<td><%=user.getno()%></td><td><%=user.getname()%></td><td><%=user.getDS()%></td><td><%=user.getDB()%></td><td><%=user.getjava()%></td><td><%=user.getweb()%></td>
</tr>
</table>
</div>
<% }
else{
%>
<p align="center">系统中没有学号为<%=user.getno()%>的成绩!</p>
<%} %>
</body>
</html>
DBmanger
package aaa;
/**
*
* @author YANG
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class DBManager extends HttpServlet {
ServletConfig config;
private static String UserName="root";
private static String Password="123456";
private static String url="jdbc:mysql://localhost:3306/score";
private static Connection connection;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
this.config = config;
url = config.getInitParameter("ConnectionURL");
}
public static Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(url, UserName, Password);
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | SQLException ex) {
Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
}
return connection;
}
public static void closeAll(Connection connection, Statement statement,
ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException ex) {
Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
UserDAO
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package aaa;
/**
*
* @author YANG
*/
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Administrator
*/
public class UserDAO {
static int i=0;
public static User queryUser1(String no) {
Connection connection = DBManager.getConnection();
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
StringBuilder sqlStatement = new StringBuilder();
sqlStatement.append("SELECT * FROM Score WHERE no=?");
try {
preparedStatement = connection.prepareStatement(sqlStatement.toString());
preparedStatement.setString(1, no);
resultSet = preparedStatement.executeQuery();
User user = new User();
if (resultSet.next()) {
user.setno(resultSet.getString("no"));
user.setname(resultSet.getString("name"));
user.setDS(resultSet.getInt("DS"));
user.setDB(resultSet.getInt("DB"));
user.setjava(resultSet.getInt("Java"));
user.setweb(resultSet.getInt("Web"));
return user;
} else {
return null;
}
} catch (SQLException ex) {
Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
return null;
} finally {
DBManager.closeAll(connection, preparedStatement, resultSet);
}
}
}
User
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package aaa;
/**
*
* @author YANG
*/
public class User {
private String no;
private String name;
private int DS;
private int DB;
private int java;
private int web;
public void setno(String no) {
this.no=no;
}
public String getno() {
return no;
}
public void setname(String name) {
this.name=name;
}
public String getname() {
return name;
}
public void setDS(int DS) {
this.DS=DS;
}
public int getDS() {
return DS;
}
public void setDB(int DB) {
this.DB=DB;
}
public int getDB() {
return DB;
}
public void setjava(int java) {
this.java=java;
}
public int getjava() {
return java;
}
public void setweb(int web) {
this.web=web;
}
public int getweb() {
return web;
}
public void queryUser() {
User user=UserDAO.queryUser1(this.no);
if(user.name!=null) {
this.name = user.getname();
this.DS = user.getDS();
this.DB = user.getDB();
this.java =user.getjava();
this.web = user.getweb();
return;
}
}
}
界面展示