📄 dbbean.java
字号:
package csmyzsweb;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
//初始化bean,创建数据库连接。
public class DBbean {
public Connection conn=null;
public DBbean()throws SQLException,ClassNotFoundException {
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}catch(ClassNotFoundException ee){throw ee;}
try {
conn = DriverManager.getConnection
("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=CivilCollege;User=;Password=");
} catch (SQLException ee) { throw ee;}
}
//登录处理函数,登陆录成功返回网报号,否则返回空值
public String logincheck(String NetworkCode, String password) throws SQLException{
String ExaminationCode = null;
try{java.sql.Statement stmt=conn.createStatement();
ResultSet rs= stmt.executeQuery("select ExaminationCode from ExaminationStudent where NetworkCode='"
+NetworkCode+"' and password='"+password+"'");
if (rs.next())
{ExaminationCode=rs.getString(1);
return ExaminationCode;}
else
{ return null;}
}catch(SQLException e){ throw e; }
}
//插入数据:注册时调用,用于插入考生号等。
public String insertdata(String ExaminationCode,String Password,
String Question,String Answer) throws SQLException{
String sqlstr="insert into ExaminationStudent( ExaminationCode,Password, NetworkCode,"+
" Question,Answer) values(?,?,?,?,?)";
String NetworkCode=getNetworkCode();
if ((NetworkCode==null) || (NetworkCode.equals(""))){
return null;
}
try{
java.sql.PreparedStatement pstmt=conn.prepareStatement(sqlstr);
pstmt.setString(1,ExaminationCode);
pstmt.setString(2,Password);
pstmt.setString(3,NetworkCode);
if( Question==null || Question.equals(""))
pstmt.setNull(4,Types.VARCHAR);
else
pstmt.setString(4,Question);
if( Answer==null || Answer.equals(""))
pstmt.setNull(5,Types.VARCHAR);
else
pstmt.setString(5,Answer);
pstmt.executeUpdate();
return NetworkCode;
}catch(SQLException ee){
throw ee;}
}
//获取网报号,成功返回五位数字串,否则返回null
private String getNetworkCode(){
String sqlstr="select max(CONVERT (int, NetworkCode)) from ExaminationStudent";
String str=null;
try{
java.sql.Statement stmt=conn.createStatement();
ResultSet rs= stmt.executeQuery(sqlstr);
if(!rs.next()){return "000000";}
int recount=rs.getInt(1)+1;
str=Integer.toString(recount);
int i=str.length();
for(int j=5;j>i;j--){
str="0"+str;
}
}catch(SQLException ee){return null;}
return str;
}
//更新数据,更新成功返回true,异常返回false.调用前检查Session是否已登录。
public boolean updatedata(String City,String County,String MiddleSchool,String SpecialtyType,
String StudentName,String Sex,String Nationality,
String Birth,String PoliticsVisage,String IdentityCardID,
String ExamineeType,String ResidenceCity,String ResidenceRounty,
String ResidenceVillages,String Telephone,String MobileTelephone,
String PostCode,
String Address,String Addressee,String EnterSpecialityOne,
String EnterSpecialityTwo,String Adjust,String Resume,String StrongSuit,
String EncouragementAndPunishment,String ExaminationCode)throws SQLException{
String sqlstr="update ExaminationStudent "+
" set City=?,County=?,MiddleSchool=?, SpecialtyType=?,"+
" StudentName=?,Sex=?,Nationality=?,"+
" Birth=?,PoliticsVisage=?,IdentityCardID=?,"+
" ExamineeType=?,ResidenceCity=?,ResidenceRounty=?,"+
" ResidenceVillages=?,Telephone=?,MobileTelephone=?,"+
" PostCode=?,Address=?,Addressee=?,EnterSpecialityOne=?,"+
" EnterSpecialityTwo=?,Adjust=?,Resume=?,StrongSuit=?,"+
" EncouragementAndPunishment=?,FillInDate=GETDATE() where ExaminationCode=?";
try{ java.sql.PreparedStatement pstmt=conn.prepareStatement(sqlstr);
pstmt.setString(1,City);
pstmt.setString(2,County);
pstmt.setString(3,MiddleSchool);
pstmt.setString(4,SpecialtyType);
pstmt.setString(5,StudentName);
pstmt.setString(6,Sex);
pstmt.setString(7,Nationality);
pstmt.setString(8,Birth);
pstmt.setString(9,PoliticsVisage);
pstmt.setString(10,IdentityCardID);
pstmt.setString(11,ExamineeType);
pstmt.setString(12,ResidenceCity);
pstmt.setString(13,ResidenceRounty);
pstmt.setString(14,ResidenceVillages);
if(Telephone==null || Telephone.equals(""))
pstmt.setNull(15,Types.VARCHAR);
else
pstmt.setString(15,Telephone);
if(MobileTelephone==null || MobileTelephone.equals(""))
pstmt.setNull(16,Types.VARCHAR);
else
pstmt.setString(16,MobileTelephone);
pstmt.setString(17,PostCode);
pstmt.setString(18,Address);
pstmt.setString(19,Addressee);
pstmt.setString(20,EnterSpecialityOne);
pstmt.setString(21,EnterSpecialityTwo);
pstmt.setString(22,Adjust);
pstmt.setString(23,Resume);
if(StrongSuit==null || StrongSuit.equals(""))
pstmt.setNull(24,Types.VARCHAR);
else
pstmt.setString(24,StrongSuit);
if(EncouragementAndPunishment==null || EncouragementAndPunishment.equals(""))
pstmt.setNull(25,Types.VARCHAR);
else
pstmt.setString(25,EncouragementAndPunishment);
pstmt.setString(26,ExaminationCode);
int i=pstmt.executeUpdate();
if(i==1){return true;}else {return false;}
}catch(SQLException e){throw e;}
}
//删除报考信息,必须是登录成功后才可调用。调用前先检查session是否已登录。
public boolean deldata(String ExaminationCode)throws SQLException{
String sqlstr="delete from ExaminationStudent where ExaminationCode=?";
try{
java.sql.PreparedStatement pstmt=conn.prepareStatement(sqlstr);
pstmt.setString(1,ExaminationCode);
int i= pstmt.executeUpdate();
if(i==1){ return true;}
else { return false;}
}catch(SQLException e){throw e;}
}
//按问题和答案查找密码。找到返回密码,否则返回空
public String getExampassword(String ExaminationCode,String Question,String Answer)
throws SQLException{
String passwordandnetcode=null;
String sqlstr="select [password] ,NetworkCode from ExaminationStudent where ExaminationCode=? and Question=? and Answer=?";
if(Question==null || Question.equals("")){return null;}
if(Answer==null || Answer.equals("")){return null;}
try{
java.sql.PreparedStatement pstmt=conn.prepareStatement(sqlstr);
pstmt.setString(1,ExaminationCode);
pstmt.setString(2,Question);
pstmt.setString(3,Answer);
ResultSet rs=pstmt.executeQuery();
if(rs.next()) {
passwordandnetcode="考生"+ExaminationCode+"<br>你的密码是:"+rs.getString(1)+"<br>你的网报号是:"+rs.getString(2);
return passwordandnetcode;}
else
return null;
}catch(SQLException e){throw e;}
}
//修改密码,根据旧密码和考生号定位记录
public boolean changepassword(String NetworkCode,String oldpassword,String newpassword)
throws SQLException{
String sqlstr="update ExaminationStudent set password=? where NetworkCode=? and password=? ";
try{
java.sql.PreparedStatement pstmt=conn.prepareStatement(sqlstr);
pstmt.setString(1,newpassword);
pstmt.setString(2,NetworkCode);
pstmt.setString(3,oldpassword);
int i= pstmt.executeUpdate();
if(i==1){ return true;}
else { return false;}
}catch(SQLException e){throw e;}
}
//-----按SQL串获取一个数据集-通用方法
public ResultSet getResultSet(String sqlstr) throws SQLException{
try{
java.sql.PreparedStatement pstmt=conn.prepareStatement(sqlstr);
ResultSet rs=pstmt.executeQuery();
return rs;
}catch(SQLException e){throw e;}
}
//--按考生号获取该考生的详细资料
public ResultSet getOneResultSet(String ExaminationCode) throws SQLException{
String sqlstr="select * from ExaminationStudent where ExaminationCode=?";
try{
java.sql.PreparedStatement pstmt=conn.prepareStatement(sqlstr);
pstmt.setString(1,ExaminationCode);
ResultSet rs=pstmt.executeQuery();
return rs;
}catch(SQLException e){throw e;}
}
//----获取考生报名数量
public int getExamstudentcount() throws SQLException{
int count=0;
String sqlstr="select count(*) from ExaminationStudent where StudentName is not null";
try{
java.sql.PreparedStatement pstmt=conn.prepareStatement(sqlstr);
ResultSet rs=pstmt.executeQuery();
if( rs.next()) {count=rs.getInt(1);}
}catch(SQLException e){throw e;}
return count;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -