Vote.zip

  • congzz
    了解作者
  • Java
    开发工具
  • 2MB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2020-11-20 12:01
    上传日期
投票系统: - 投票项目管理 -投票管理 -用户信息管理
Vote.zip
内容介绍
package com.itell.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import com.itell.bean.Subject; import java.util.List; import com.itell.utill.Dbhelper; public class SubjectDao { private Connection connection; public SubjectDao() { connection=Dbhelper.getConn(); // try { // Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // String url="jdbc:sqlserver://localhost:1433;DatabaseName=Survey"; // String user="sa"; // String password="123"; // connection = DriverManager.getConnection(url,user,password); // System.out.println(connection); // System.out.println("连接数据库成功!"); // }catch(ClassNotFoundException e) { // e.printStackTrace(); // System.out.print("SQL Server连接失败1!"); // }catch(SQLException e) { // e.printStackTrace(); // System.out.print("SQL Server连接失败2!"); // } } public void SaveSubject(Subject subject) { PreparedStatement ps = null; try { ps=connection.prepareStatement("INSERT INTO Survey.dbo.t_subject(title,number,starttime,endtime,userId) VALUES(?,?,?,?,?)"); ps.setString(1, subject.getTitle()); ps.setInt(2, subject.getNumber()); ps.setLong(3, subject.getStarttime()); ps.setLong(4, subject.getEndtime()); ps.setLong(5,subject.getUseId()); ps.execute(); System.out.println("保存调查主题信息成功"); }catch (SQLException e) { e.printStackTrace(); } finally { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } } public void DeleteSubjectById(Long id) { PreparedStatement ps=null; try { ps=connection.prepareStatement("delect from Survey.dbo.t_subject where id=?"); ps.setLong(1, id); ps.execute(); System.out.println("删除调查主题信息成功"); }catch (SQLException e) { e.printStackTrace(); }finally { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } } public Subject FinfSubjectById(Long id) { Subject subject=null; PreparedStatement ps = null; try { ps=connection.prepareStatement("select * from Survey.dbo.t_subject where id=?"); ps.setLong(1, id); ResultSet set = ps.executeQuery(); while(set.next()) { subject=new Subject(); subject.setId(set.getLong(1)); subject.setTitle(set.getString(2)); subject.setNumber(set.getInt(3)); subject.setStarttime(set.getLong(4)); subject.setEndtime(set.getLong(5)); subject.setUseId(set.getLong(6)); } }catch (SQLException e) { e.printStackTrace(); } finally { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } return subject; } public Subject FinfSubjectByStarttime(Long starttime) { Subject subject=null; PreparedStatement ps = null; try { ps=connection.prepareStatement("select * from Survey.dbo.t_subject where starttime=?"); ps.setLong(1, starttime); ResultSet set = ps.executeQuery(); while(set.next()) { subject=new Subject(); subject.setId(set.getLong(1)); subject.setTitle(set.getString(2)); subject.setNumber(set.getInt(3)); subject.setStarttime(set.getLong(4)); subject.setEndtime(set.getLong(5)); subject.setUseId(set.getLong(6)); } }catch (SQLException e) { e.printStackTrace(); } finally { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } return subject; } public long FinfSubjectMaxId() { long id=0; Statement statement = null; try { statement = connection.createStatement(); ResultSet set = statement.executeQuery("select MAX(id) from Survey.dbo.t_subject"); while (set.next()) { id=set.getLong(1); } }catch (SQLException e) { e.printStackTrace(); } finally { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } return id; } public Subject FinfSubjectByUseId(Long userid) { Subject subject=null; PreparedStatement ps = null; try { ps=connection.prepareStatement("select * from Survey.dbo.t_subject where userId=?"); ps.setLong(1, userid); ResultSet set = ps.executeQuery(); while(set.next()) { subject=new Subject(); subject.setId(set.getLong(1)); subject.setTitle(set.getString(2)); subject.setNumber(set.getInt(3)); subject.setStarttime(set.getLong(4)); subject.setEndtime(set.getLong(5)); subject.setUseId(set.getLong(6)); } }catch (SQLException e) { e.printStackTrace(); } finally { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } return subject; } public Subject FinfSubjectByTitle(String title) { Subject subject=null; PreparedStatement ps = null; try { ps=connection.prepareStatement("select * from Survey.dbo.t_subject where title=?"); ps.setString(1, title); ResultSet set = ps.executeQuery(); while(set.next()) { subject=new Subject(); subject.setId(set.getLong(1)); subject.setTitle(set.getString(2)); subject.setNumber(set.getInt(3)); subject.setStarttime(set.getLong(4)); subject.setEndtime(set.getLong(5)); subject.setUseId(set.getLong(6)); } }catch (SQLException e) { e.printStackTrace(); } finally { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } return subject; } public List<Subject> FindAllSubject(){ Statement statement = null; ArrayList<Subject> list = new ArrayList<Subject>(); try { statement = connection.createStatement(); ResultSet set = statement.executeQuery("select * from Survey.dbo.t_subject"); while (set.next()) { Subject subject= new Subject(); subject.setId(set.getLong("id")); subject.setTitle(set.getString("title")); subject.setNumber(set.getInt("number")); subject.setStarttime(set.getLong("starttime")); subject.setEndtime(set.getLong("endtime")); subject.setUseId(set.getLong("userId")); list.add(subject); System.out.println("查找成功"); } } catch (SQLException e) { e.printStackTrace(); } finally { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } return list; } }
评论
    相关推荐