package cn.jhc.service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import cn.jhc.bean.Group;
import cn.jhc.bean.User;
public class SurveryService {
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static class SexCount{
public int maleCount;
public int femaleCount;
public int getMaleCount(){
return maleCount;
}
public void setMaleCount(int maleCount){
this.maleCount = maleCount;
}
public int getFemaleCount(){
return maleCount;
}
public void setFemaleCount(int maleCount){
this.maleCount = maleCount;
}
}
public static SexCount calculateSexCount(){
Connection conn = null;
ResultSet rs = null;
Statement stat = null;
SexCount sexcount =new SexCount();
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=123456");
} catch (SQLException e) {
// TODO: handle exception
}
String sql = "select "+
"sum(case option_value when 'male' then 1 else 0 end) as malecount,"+
"sum(case option_value when 'female' then 1 else 0 end) as femalecount "+
"from survey " +
"where option_name='sex'";
try {
stat= conn.createStatement();
rs = stat.executeQuery(sql);
if (rs.first()) {
sexcount.maleCount = rs.getInt("malecount");
sexcount.femaleCount= rs.getInt("femalecount");
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
finally{
try{
rs.close();
stat.close();
conn.close();
}
catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
return sexcount;
}
public static int findIdByUserneme(String username){
ResultSet rs = null;
Statement stat = null;
int value = 0;
Connection conn = getConnection();
String sql = "select id from user where username='"+username+"'";
try {
stat= conn.createStatement();
rs = stat.executeQuery(sql);
if (rs.first()) {
value = rs.getInt(1);
}else {
value = 0;
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
finally{
try{
rs.close();
stat.close();
conn.close();
}
catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
return 0;
}
public static void saveOPtion(int userId,
String optionName, String optionValue) {
ResultSet rs = null;
Statement stat = null;
int value = 0;
Connection conn = getConnection();
String sql = "insert into survey(option_name,option_value,user_id)"
+"values('"+ optionName + "','"+ optionValue +"',"+userId +")";
try {
stat= conn.createStatement();
stat.execute(sql);
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
finally{
try{
stat.close();
conn.close();
}
catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
System.out.println("a");
}
public static List<Group> getGroups() {
List<Group> list= new ArrayList<Group>();
Connection connection = getConnection();
ResultSet rs =null;
Statement statement = null;
String sql = "select id,name from `group`";
try {
statement = connection.createStatement();
rs = statement.executeQuery(sql);
while(rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
list.add(new Group(id, name));
}
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
statement.close();
connection.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
return list;
}
public static void addUser(User user) {
Connection connection = getConnection();
Statement statement = null;
String sql = "insert into user(username,password,email,group_id)"
+"values('"+user.getName()+"','"+user.getPassword()+"',"
+"'"+user.getEmail()+"',"+user.getGroupId()+")";
try {
statement=connection.createStatement();
statement.executeUpdate(sql);
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
finally {
try {
statement.close();
connection.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
private static Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=123456");
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
return conn;
}
}
JSP用户注册登录界面
3星 · 超过75%的资源 需积分: 33 199 浏览量
2014-06-10
14:58:35
上传
评论 3
收藏 1.2MB ZIP 举报
baidu_16385315
- 粉丝: 0
- 资源: 1
最新资源
- apk.tw_LineLite_v8a_v.2.17.1_sign.apk
- Elasticsearch实战:构建高效搜索系统的秘诀.zip
- HTML+CSS+JS网页设计:从入门到精通.zip
- 数据库课程设计:从理论到实践的全面指南.zip
- Python闭包:深入理解与应用场景解析.zip
- Win64OpenSSL-3-3-0.exe
- 课高分程设计-基于C++实现的民航飞行与地图简易管理系统-南京航空航天大学
- 航天器遥测数据故障检测系统python源码+文档说明+数据库(课程设计)
- 北京航空航天大学操作系统课设+ppt+实验报告
- 基于Vue+Echarts实现风力发电机中传感器的数据展示监控可视化系统+源代码+文档说明(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈