我有一张调查表,里面有六道选择题,每道题有A--->F六个选项.
要求:统计每道题中每个选项的被选择次数(比如我要知道第三题中E选项被选了多少次)
1.数据库6个字段,分别对应6道题
2.6个选择题,一个选择题N个单选框radio,VALUE=A,VALUE=B,VALUE=C对应数据库的第N个字段,比如:第一题选A,吧A存到第一个字段
3.比如我要知道第三题中E选项被选了多少次,select count(*) from table where table.c=''C
JSP页面
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body >
<form id="form" id="form" action="${pageContext.request.contextPath}/aaa/ccc">
<table>
<tr>
第一题
</tr>
<tr>
<td>
<input type="radio" name="D1" value="A">A
</td>
<td>
<input type="radio" name="D1" value="B">B
</td>
<td>
<input type="radio" name="D1" value="C">C
</td>
</tr>
<tr>
第二题
</tr>
<tr>
<td>
<input type="radio" name="D2" value="A">A
</td>
<td>
<input type="radio" name="D2" value="B">B
</td>
<td>
<input type="radio" name="D2" value="C">C
</td>
</tr>
<tr>
<input type="submit">
</tr>
</table>
</body>
</html>
控制层
@Controller
@RequestMapping("/aaa")
public class A {
@Resource
private CService cService;
@RequestMapping(value="/ccc",method=RequestMethod.GET)
public void ccc(@RequestParam Map<String, String> params) throws SQLException {
cService.Add(params);
}
}
service层
@Service
public class CService {
@Resource
private Dao dao;
public void Add(Map<String, String> params) throws SQLException{
String D1=params.get("D1");
String D2=params.get("D2");
String sql="insert into table(D1,D2) values(?,?)";
dao.update(sql, new Object[]{D1,D2});
}
}
给你一个封装的JDBC
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Basedao {
private final String URL = "jdbc:mysql://localhost:3306/new";
private final String USER = "root";
private final String PWD = "root";
public Connection getConnection() throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(URL, USER, PWD);
return con;
}
public void close(ResultSet rs,PreparedStatement pst,Connection con) throws SQLException{
if(rs != null){
rs.close();
}
if(pst != null){
pst.close();
}
if(con != null){
con.close();
}
}
}
public class Jigou extends Basedao{
public List cha(String a){
List list=new ArrayList();
Connection con=null;
PreparedStatement pst=null;
ResultSet re=null;
try{
con=super.getConnection();
pst=con.prepareStatement("SELECT * FROM org_management
WHERE org_name
=?");
pst.setString(1, a);
re=pst.executeQuery();
while(re.next()){
ORG org=new ORG();
org.setId(re.getInt(1));
org.setName(re.getString(2));
org.setState(re.getString(3));
org.setManage(re.getString(4));
list.add(org);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
super.close(re, pst, con);
}catch(Exception e){
e.printStackTrace();
}
}
return list;
}
}