码迷,mamicode.com
首页 > 其他好文 > 详细

04课堂测试-增加信息

时间:2018-01-25 11:09:06      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:pack   exception   rom   user   jdbc   block   资源   oct   val   

 


 

 

package pers.sun.DataBase;

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

 


public class Data {   
 
 public static Connection getConnection() {
  //
加载驱动
  try {
   Class.forName("com.mysql.jdbc.Driver").newInstance();
  } catch (InstantiationException e) {
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }
  
  String root="root";
  String password="sunyu";
  String url="jdbc:mysql://localhost:3306/user_message";
  //
链接对象
  Connection con=null;
  try {
   con=DriverManager.getConnection(url,root,password);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  
  return con;
  
 }
 
 //
关闭资源
 public static void close(Connection con) {
   try {
    if(con!=null)
     con.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
 }
 
 public static void close(PreparedStatement pre) {
  try {
   if(pre!=null)
    pre.close();
  } catch (SQLException e) {
   e.printStackTrace();
  }
 } 
 
 public static void close(ResultSet result) {
  try {
   if(result!=null)
    result.close();
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
}

 

package pers.sun.user;

 

public class Teacher {
 private String classname;
 private String tename;
 private String teplace;
 
 public String getClassname() {
  return classname;
 }
 public void setClassname(String classname) {
  this.classname = classname;
 }
 public String getTename() {
  return tename;
 }
 public void setTename(String tename) {
  this.tename = tename;
 }
 public String getTeplace() {
  return teplace;
 }
 public void setTeplace(String teplace) {
  this.teplace = teplace;
 }
 
}

 

package pers.sun.user;

 

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

 

import pers.sun.DataBase.Data;

 

public class UserTool {
//1
添加用户
 public static void add(User tuser) {
  //
获得链接对象
  Connection con=Data.getConnection();
  //
插入
  String sql="insert into user_infor(username,password) value(?,?)";
  //
语句传输对象
  PreparedStatement pre=null;
  try {
   pre=con.prepareStatement(sql);
   //
写进表
   pre.setString(1, tuser.getUsername());
   pre.setString(2, tuser.getPassword());
   //????
刷新???
   pre.executeUpdate();
  } catch (SQLException e) {
   e.printStackTrace();
  }finally {
   Data.close(pre);
   Data.close(con);
  }
 }
 public static void add(Teacher teacher) {
  //
获得链接对象
  Connection con=Data.getConnection();
  //
插入
  String sql="insert into teacher_infor(classname,teachername,teachplace) value(?,?,?)";
  //
语句传输对象
  PreparedStatement pre=null;
  try {
   pre=con.prepareStatement(sql);
   //
写进表
   pre.setString(1, teacher.getClassname());
   pre.setString(2, teacher.getTename());
   pre.setString(3, teacher.getTeplace());
   //????
刷新???
   pre.executeUpdate();
  } catch (SQLException e) {
   e.printStackTrace();
  }finally {
   Data.close(pre);
   Data.close(con);
  }
  
 }
}

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="pers.sun.user.*" %>
<%@ page import="pers.sun.judge.*" %>
<!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>Insert title here</title>
</head>
<body>
<%
 
 String classnamex=request.getParameter("classname");
 String tenamex=request.getParameter("tename");
 String placex=request.getParameter("place");
 
 boolean valuex=ValueData.valueNull(request, new String[]{"classname","tename","place"});
 if(!valuex)
 {
%> 
 <jsp:forward page="loginshow.jsp"></jsp:forward>
<%
 }
 Teacher tea=new Teacher();
 tea.setClassname(classnamex);
 tea.setTename(tenamex);
 tea.setTeplace(placex);
 
 UserTool.add(tea);
 
%>
<jsp:forward page="xinxi.jsp"></jsp:forward>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="pers.sun.judge.*" %>
<%@ page import="java.util.*" %>
<!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>
用户登陆</title>
</head>
<body>
<form action="loginhandl.jsp" method="post">
 <table align="center" border="1" width="500">
  <tr>
   <td>
课程名称</td>
   <td><input type="text" name="classname">
   <%=ValueData.showError(request,"classname") %>
   </td>
  </tr>
  <tr>
   <td>
任课教师</td>
   <td><input type="text" name="tename">
   <%=ValueData.showError(request,"tename") %>
   </td>
  </tr>
  <tr>
   <td>
上课地点</td>
   <td><input type="text" name="place">
   <%=ValueData.showError(request,"place") %>
   </td>
  </tr>
  <tr align="center">
   <td colspan="2">
   <input type="submit" value="
保存">
   <input type="reset" value="
重置">
   </td>
  </tr>
 </table>
</form>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
 <h1 align="center">
添加成功!</h1>
</body>
</html>

 

 

 

1.        四则运算二

 

java文件

 

package pers.sun.operateion;
//
产生一个算式,及相应的结果
public class Operated {

 

   private int resultx;
 
  private String formulax;
 
 public int getResultx() {
  
  return resultx;
 }
 
 public void setResultx(int resultx) {
 
   this.resultx = resultx;
 }
 public String getFormulax() {
 
   return formulax;
 }
 public void setFormulax(String formula) {
 
   this.formulax=formula;
 }
 public String calculation() {
  
  
  int first=(int) (Math.random()*10+1);
  
  int second=(int) (Math.random()*10+1);
  
  int op=(int) (Math.random()*4+1);
  
  char operator = 0;
  
  switch(op) {
  
  case 1:operator=‘+‘;resultx=first+second;break;
  
  case 2:operator=‘-‘;resultx=first-second;break;
 
   case 3:operator=‘*‘;resultx=first*second;break;
 
   case 4:operator=‘/‘;break;
 
   }
  //
是除
  if(op==4) {
   //
分母不为0 且能除尽
   if(second!=0) {
    int res=first%second;
    if(res==0) {
   
    formulax=first+" "+operator+" "+second+" =";
   
    resultx=first/second;
    }
    else
   
    formulax=null;
   }
   else
   
   formulax=null;
  }
  //
不是除
  else {
  
   formulax=first+" "+operator+" "+second+" =";
  }
  
  return formulax;
 }
}

 

package pers.sun.operateion;

 

import pers.sun.operateion.Operated;
//
产生N个算式,及结果
public class ApplyIt {

 

 private int[] result;
 
 public String[] make(int n) {
  //
接收的容器
           
  String formulas[]=new String[n];
  result=new int[n];
  
  //
产生算式
  Operated opera=new Operated();
  for(int i=0;i<n;) {
   String temp=opera.calculation();
   //
判断算式是否为null
   if(temp!=null) {
          
    formulas[i]=temp;
    result[i]=opera.getResultx();
    i++;
   }
  }
  return formulas;
 }
 
 
 public int[] getResult() {
  return result;
 }
 public void setResult(int[] result) {
  this.result = result;
 }
}

 

package pers.sun.sql;

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

 

public class DB {

 

 public static Connection getConnection() {
  try {
   Class.forName("com.mysql.jdbc.Driver");
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  String root="root";
  String password="sunyu";
  String url="jdbc:mysql://localhost:3306/user_message";
  
  Connection con=null;
  try {
   con=DriverManager.getConnection(url,root,password);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  return con;
 }
 
 public static void close(Connection con) {
  try {
   if(con!=null)
    con.close();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }
 public static void close(PreparedStatement pre) {
  try {
   if(pre!=null)
    pre.close();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public static void close(ResultSet result) {
  try {
   if(result!=null)
    result.close();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

 

package pers.sun.sql;

 

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

 

import pers.sun.operateion.Operated;

 

public class SqlTool {
 
 public static void add(String formula,int result) {
  if(formula!=null) {
   String sql="insert into math(formula,result) value(?,?)";
   Connection connection=DB.getConnection();
   PreparedStatement preparedstatement=null;
   try {
    preparedstatement=connection.prepareStatement(sql);
    preparedstatement.setString(1,formula);
    preparedstatement.setInt(2,result);
    preparedstatement.executeUpdate();
    
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }finally {
    DB.close(preparedstatement);
    DB.close(connection);
   }
   
  }
 }
 public static void deleted() {
  
 }
 public static List<Operated> search() {
  
  Connection connection=DB.getConnection();
  String sql="select * from math";
  PreparedStatement pre=null;
  ResultSet result=null;
  
  List<Operated> op= new ArrayList<Operated>();
  
  try {
   pre = connection.prepareStatement(sql);
   result=pre.executeQuery();
   while(result.next()) {
    Operated temp=new Operated();
    temp.setResultx(result.getInt("result"));
    temp.setFormulax(result.getString("formula"));
    op.add(temp);
   }
   
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return op;
  
 }
 public static void updata() {
  
 }
}

 

JSP文件

 

<%@ page language="java" contentType="text/html; UTF-8"
    pageEncoding="UTF-8"%>
<!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>
计算准备</title>
</head>
<body>
 <form action="outputbegin.jsp" method="post">
  <table align="center" border="1" width="100">
   <tr>
   <!--
没有判断输入的是否为整数不为空 -->
    <td>
输入你要做的题</td>
   </tr>
   <tr>
    <td>
     <input type="text" name="number" />
    </td>
   </tr>
   <tr>
    <td>
     <input type="submit" value="
确定" name="submit" />
     <input type="reset" value="
重置" name="reset" />
    </td>
   </tr>
  </table>
  
 </form>
</body>
</html>

 

<%@page import="pers.sun.sql.SqlTool"%>
<%@ page language="java" contentType="text/html; UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="pers.sun.util.*" %>
<%@page import="pers.sun.operateion.*" %>
<%@page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--
接收用户数字-->
<html>
<head>
 <title>
算式表格</title>
</head>
<body>
<%
 //
接收信息
 int num=Integer.parseInt(request.getParameter("number"));
 ApplyIt apply =new ApplyIt();
 //
生成算式+结果
 String[] suanshi=apply.make(num);
 int rightresults[]=apply.getResult();
 
 //
写入数据库
 for(int i=0;i<num;i++){
  SqlTool.add(suanshi[i], rightresults[i]);
 }
%>
 <form action="handle.jsp" method="post">
 <table align="center" border="1" width="500">
  <tr>
   <td>
算式</td>
   <td>
结果</td>
  </tr>
  <%
   for(String stemp:suanshi){
  %> 
  <tr>
   <td><%=stemp %></td>
   <td><input type="text" value="" name="peoresult" /></td>
  </tr>
  <%
   }
  %>
  <tr>
    <td><input type="submit" value="
提交" name="submit" /></td>
  </tr>
  </table>
 </form>

 

</body>

 

<%@page import="pers.sun.sql.SqlTool"%>
<%@page import="pers.sun.operateion.Operated"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
<!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>
判断结果</title>
</head>
<body>
  
  <ul>
   <li> <a href="begin.jsp" >
重来</a>&nbsp;&nbsp;</li>
   <li> <a href="ending.jsp">
结束</a></li>
  </ul>
  <hr>
  <table align="center" border="1" width="500">
   <tr>
    <td>
算式</td>
    <td>
你的答案</td>
    <td>
对错</td>
    <td>
正确答案</td>
   </tr>
<%
  List<Operated> operation=new ArrayList<Operated>();
  int length=operation.size();
  //String[] peoresult=new String[length];
  //peoresult=request.getParameterValues("peoresult");
  operation=SqlTool.search();
  int count=0;
  System.out.print(request.getParameter("peoresult"));
  for(Operated temp:operation){
   int i=0;
   String peoresultx=request.getParameter("peoresult");
%>
  <tr>
   <td><%=temp.getFormulax() %></td>
   <td><%=peoresultx %></td>
   <%
   String correct=null;
   if(peoresultx!=null&&!"".equals(peoresultx.trim())){
    if(Integer.parseInt(peoresultx)==temp.getResultx()){
     correct="
";
     count++;
    }
    else
     correct="
";
   }
   %>
   <td><%=correct %></td>
   <td><%=temp.getResultx() %></td>
  </tr>
<%  
  }
%> 
   <tr><td>
你的得分:<%=count %></td></tr>
  </table>

 

</body>
</ht

 

 

 

 

04课堂测试-增加信息

标签:pack   exception   rom   user   jdbc   block   资源   oct   val   

原文地址:https://www.cnblogs.com/liushiqiang123/p/8349765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!