1 package com.jaovo.msg.dao;
2
3 import java.sql.Connection;
4 import java.sql.PreparedStatement;
5 import java.sql.SQLException;
6
7 import com.jaovo.msg.model.Shizi;
8 import com.jaovo.msg.util.DBUtil;
9
10 public class MangageShizi {
11
12 public void add(Connection connection,Shizi shizi)
13 {
14 String sql="insert into shi_s(firstnumber,secondnumber,characte,realanswer) value (?,?,?,?)";
15
16 PreparedStatement preparedStatement=null;
17 try {
18 preparedStatement = connection.prepareStatement(sql);
19 preparedStatement.setInt(1,shizi.getFirstNumber());
20 preparedStatement.setInt(2, shizi.getSecondNumber());
21 preparedStatement.setString(3, shizi.getCharacter());
22 preparedStatement.setInt(4, shizi.getRealAnswer());
23 preparedStatement.executeUpdate();
24
25 } catch (SQLException e) {
26 // TODO Auto-generated catch block
27 e.printStackTrace();
28 }finally {
29 DBUtil.close(preparedStatement);
30 //DBUtil.close(connection);
31
32 }
33 }
34
35 }
1 package com.jaovo.msg.model;
2
3 public class Shizi {
4 private int firstNumber;
5 private int secondNumber;
6 private int maybeThirdNumber;
7 private int answer;
8 private int realAnswer;
9 private String character;
10 private boolean rightOrWrong;
11 public int getMaybeThirdNumber() {
12 return maybeThirdNumber;
13 }
14 public void setMaybeThirdNumber(int maybeThirdNumber) {
15 this.maybeThirdNumber = maybeThirdNumber;
16 }
17 public int getAnswer() {
18 return answer;
19 }
20 public void setAnswer(int answer) {
21 this.answer = answer;
22 }
23 public int getRealAnswer() {
24 return realAnswer;
25 }
26 public void setRealAnswer(int realAnswer) {
27 this.realAnswer = realAnswer;
28 }
29 public int getFirstNumber() {
30 return firstNumber;
31 }
32 public void setFirstNumber(int firstNumber) {
33 this.firstNumber = firstNumber;
34 }
35 public int getSecondNumber() {
36 return secondNumber;
37 }
38 public void setSecondNumber(int secondNumber) {
39 this.secondNumber = secondNumber;
40 }
41 public String getCharacter() {
42 return character;
43 }
44 public void setCharacter(String character) {
45 this.character = character;
46 }
47 public boolean isRightOrWrong() {
48 return rightOrWrong;
49 }
50 public void setRightOrWrong(boolean rightOrWrong) {
51 this.rightOrWrong = rightOrWrong;
52 }
53
54 }
1 package com.jaovo.msg.servlet;
2
3 import java.io.IOException;
4 import java.sql.Connection;
5 import java.util.Random;
6
7 import javax.servlet.RequestDispatcher;
8 import javax.servlet.ServletException;
9 import javax.servlet.annotation.WebServlet;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13
14 import com.jaovo.msg.dao.MangageShizi;
15 import com.jaovo.msg.model.Shizi;
16 import com.jaovo.msg.util.DBUtil;
17
18 /**
19 * Servlet implementation class MyServlet
20 */
21 @WebServlet("/MyServlet")
22 public class MyServlet extends HttpServlet {
23 private static final long serialVersionUID = 1L;
24 public MyServlet() {
25 super();
26 }
27 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
28 doPost(request,response);
29 }
30 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
31 int quality = Integer.parseInt(request.getParameter("quality"));
32 Connection connection = DBUtil.getConnection();
33 MangageShizi mShizi=new MangageShizi();
34 Random random=new Random();
35 int a,b,d;
36 char ch;
37 for(int i=0;i<quality;i++)
38 {
39 d=random.nextInt(4);
40 if(d==0)
41 ch='+';
42 else if(d==1)
43 ch='-';
44 else if(d==2)
45 ch='*';
46 else
47 ch='/';
48 Shizi shizi=new Shizi();
49 shizi.setCharacter(String.valueOf(ch));
50 if(d==0) {
51 a=random.nextInt(100)+1;
52 b=random.nextInt(100-a)+1;
53 shizi.setFirstNumber(a);
54 shizi.setSecondNumber(b);
55 shizi.setRealAnswer(a+b);
56 }
57 else if(d==1) {
58 a=random.nextInt(100)+1;
59 b=random.nextInt(100)+1;
60 if(a<b) {
61 b=random.nextInt(100)+1;
62 }
63 shizi.setFirstNumber(a);
64 shizi.setSecondNumber(b);
65 shizi.setRealAnswer(a-b);
66 }
67 else if(d==2) {
68 a=random.nextInt(11);
69 b=random.nextInt(11);
70 shizi.setFirstNumber(a);
71 shizi.setSecondNumber(b);
72 shizi.setRealAnswer(a*b);
73 }
74 else {
75 a=random.nextInt(11);
76 b=random.nextInt(11);
77 a=a*b;
78 shizi.setFirstNumber(a);
79 shizi.setSecondNumber(b);
80 shizi.setRealAnswer(a/b);
81 }
82 mShizi.add(connection,shizi);
83 }
84 DBUtil.close(connection);
85 //System.out.println("鏁版嵁搴?");
86 RequestDispatcher dispatcher = request.getRequestDispatcher("doit.jsp");
87 dispatcher .forward(request, response);
88 //request.getRequestDispatcher("../doit.jsp").forward(request, response);
89 }
90
91 }
1 package com.jaovo.msg.util;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.PreparedStatement;
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8
9 public class DBUtil {
10
11 public static Connection getConnection() {
12 try {
13 Class.forName("com.mysql.jdbc.Driver").newInstance();
14 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
15 // TODO Auto-generated catch block
16 e.printStackTrace();
17 }
18 String user = "root";
19 String password = "root";
20 String url = "jdbc:mysql://localhost:3306/jaovo_msg?useUnicode=true&characterEncoding=utf-8&useSSL=false";
21 //jdbc:mysql://localhost:3306/jaovo_msg?useUnicode=true&characterEncoding=utf-8&useSSL=false
22 Connection connection = null;
23 try {
24 //2 鍒涘缓閾炬帴瀵硅薄connection
25 connection = DriverManager.getConnection(url,user,password);
26 } catch (SQLException e) {
27 // TODO Auto-generated catch block
28 e.printStackTrace();
29 }
30 return connection;
31 }
32 public static void close(Connection connection) {
33 if(connection!=null) {
34 try {
35 connection.close();
36 } catch (SQLException e) {
37 // TODO Auto-generated catch block
38 e.printStackTrace();
39 }
40 }
41 }
42
43 public static void close(PreparedStatement preparedStatement) {
44 try {
45 if (preparedStatement != null) {
46 preparedStatement.close();
47 }
48
49 } catch (SQLException e) {
50 // TODO Auto-generated catch block
51 e.printStackTrace();
52 }
53 }
54 public static void close(ResultSet resultSet) {
55 try {
56 if (resultSet != null) {
57 resultSet.close();
58 }
59
60 } catch (SQLException e) {
61 // TODO Auto-generated catch block
62 e.printStackTrace();
63 }
64 }
65
66 }
1 <%@page import="java.sql.ResultSet"%>
2 <%@page import="java.sql.Statement"%>
3 <%@page import="java.sql.Connection"%>
4 <%@page import="com.jaovo.msg.util.DBUtil"%>
5 <%@ page language="java" contentType="text/html; charset=UTF-8"
6 pageEncoding="UTF-8"%>
7 <%
8 String path = request.getContextPath();
9 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
10 %>
11 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
12 <html>
13 <head>
14 <base href="<%=basePath%>">
15 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
16 <title>答题</title>
17 </head>
18 <body>
19 <%
20 Connection connection = DBUtil.getConnection();
21 Statement statement=connection.createStatement();
22 ResultSet resultSet=statement.executeQuery("select * from shi_s");
23 int idn=0;
24 while(resultSet.next())
25 {
26 int num1=resultSet.getInt("firstnumber");
27 int num2=resultSet.getInt("secondnumber");
28 String character=resultSet.getString("characte");
29 int realanswer=resultSet.getInt("realanswer");
30 String ind=String.valueOf(realanswer);
31 if(request.getParameter(ind)!=null)
32 {
33 int answer=Integer.parseInt(request.getParameter(ind));
34 %>
35 <label for="name" ><%=num1+character+num2 %>=</label>
36 <table width="75" border="1">
37 <tr>
38 <td>
39 <%=answer %>
40 </td>
41
42 </tr>
43 </table>
44 <%
45 if(answer==realanswer)
46 {
47 %>回答正确 <%
48 }
49 else{
50 %>回答错误,正确答案为<%=realanswer %>
51 <%
52 }
53 }
54 else
55 {
56 out.print(request.getParameter(ind));
57 out.print("出错");
58 }
59
60 }
61 statement.executeUpdate("delete from shi_s");
62 %>
63
64 <p>
65 </body>
66 </html>
1 <%@page import="java.sql.ResultSet"%>
2 <%@page import="java.sql.Statement"%>
3 <%@page import="java.sql.Connection"%>
4 <%@page import="com.jaovo.msg.util.DBUtil"%>
5 <%@page import="com.jaovo.msg.model.Shizi"%>
6 <%@page import="java.util.ArrayList"%>
7 <%@page import="java.util.Random"%>
8 <%@page import="java.util.ArrayList"%>
9 <%@page import="java.util.List"%>
10 <%@ page language="java" contentType="text/html; charset=UTF-8"
11 pageEncoding="UTF-8"%>
12 <%
13 String path = request.getContextPath();
14 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
15 %>
16 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
17 <html>
18 <head>
19 <base href="<%=basePath%>">
20 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
21 <title>做题页面</title>
22 </head>
23 <body>
24 <input type="button" value="倒计时" id="counter" /><br /><br />
25 <input type="button" value="开始" id="start" />
26 <script type="text/javascript">
27 function $(id){return document.getElementById(id)}
28 var i=20,s='';
29 $('start').onclick=function(){
30 i=20;s='';
31 var t=setInterval(function(){
32 if(s!='是否重新计时?' && i>0){
33 $('counter').value='倒计时'+(i--)+'秒';
34 }else{
35 clearInterval(t)
36 if(i==0)alert('时间到')
37 }
38 },1000);
39 }
40 $('stop').onclick=function(){
41 s=$('arg').value;
42 }
43 </script>
44 <form id="form1" name="form1" method="post" action="answer.jsp">
45 <%
46 Connection connection = DBUtil.getConnection();
47 Statement statement=connection.createStatement();
48 ResultSet resultSet=statement.executeQuery("select * from shi_s");
49 int idn=0;
50 while(resultSet.next())
51 {
52 idn++;
53 int num1=resultSet.getInt("firstnumber");
54 int num2=resultSet.getInt("secondnumber");
55 String character=resultSet.getString("characte");
56 int realanswer=resultSet.getInt("realanswer");
57
58 %>
59 <label for="name">
60 <%=num1+character+num2 %>=
61 </label>
62 <input type="text" name="<%=realanswer %>" id="<%=realanswer %>" />
63 <%
64 }
65 %>
66 <input type="submit" name="提交"/>
67 </form>
68 <%
69
70 %>
71 </body>
72 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <%
4 String path = request.getContextPath();
5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
6 %>
7
8 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
9 <html>
10 <head>
11 <base href="<%=basePath%>">
12 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
13 <object height="100" width="100" data="abc.mp3"></object>
14 <title>首页</title>
15 </head>
16 <body>
17 <style type="text/css">
18 body,td,th{
19 font-size: 24px;
20 }
21 body{
22 background-color: #0CF;
23 background-image: url(a.jpg);
24 }
25
26 </style>
27 <%
28
29 %>
30 <form id="form1" name="form1" method="post" action="MyServlet">
31 多少道题 :
32 <input type="text" name="quality" size="20" />
33 <input type="submit" value="开始" />
34 </form>
35
36 </body>
37 </html>