📄 sqlserveroperatordao.java
字号:
package com.chinamobile.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import com.chinamobile.bean.OperatorBean;
public class SQLServerOperatorDAO implements OperatorDAO {
private final String INSERT_AN_OPERATOR = "insert into TOperator values(?,?,?,?)";
private final String GET_ALL_OPERATORS = "select * from TOperator order by operator_id";
private final String GET_AN_OPERATOR = "select * from TOperator where operator_id=? and operator_pwd=?";
public boolean createOperator(OperatorBean operator) {
Connection conn =null;
PreparedStatement pstmt=null;
boolean tmp=true;
try{
conn=SQLServerDAOFactory.getConnection();
pstmt=conn.prepareStatement(INSERT_AN_OPERATOR);
pstmt.setString(1, operator.getOperator_ID());
pstmt.setString(2, operator.getOperator_Name());
pstmt.setString(3, operator.getOperator_Pwd());
pstmt.setString(4, operator.getIs_Admin());
int result=pstmt.executeUpdate();
if(result!=1)
tmp = false;
}catch(SQLException e){
tmp = false;
e.printStackTrace();
}finally{
SQLServerDAOFactory.closeStatement(pstmt);
SQLServerDAOFactory.closeConnection(conn);
}
return tmp;
}
public Iterator getAllOperators() {
Connection conn =null;
PreparedStatement pstmt=null;
boolean tmp=true;
try{
conn=SQLServerDAOFactory.getConnection();
pstmt=conn.prepareStatement(GET_ALL_OPERATORS);
OperatorBean operator=null;
ResultSet rs=pstmt.executeQuery();
ArrayList operators = new ArrayList();
while(rs.next()){
operator=new OperatorBean();
operator.setOperator_ID(rs.getString("Operator_ID"));
operator.setOperator_Name(rs.getString("Operator_Name"));
operator.setOperator_Pwd(rs.getString("Operator_Pwd"));
operator.setIs_Admin(rs.getString("Is_Admin"));
operators.add(operator);
}
return operators.iterator();
}catch(SQLException e){
return null;
}finally{
SQLServerDAOFactory.closeStatement(pstmt);
SQLServerDAOFactory.closeConnection(conn);
}
}
public boolean isExists(OperatorBean operator) {
Connection conn =null;
PreparedStatement pstmt=null;
boolean tmp=true;
try{
conn=SQLServerDAOFactory.getConnection();
pstmt=conn.prepareStatement(GET_AN_OPERATOR);
pstmt.setString(1, operator.getOperator_ID());
pstmt.setString(2, operator.getOperator_Pwd());
ResultSet result=pstmt.executeQuery();
if(result.next()){
tmp = true;
}else{
tmp = false;
}
}catch(SQLException e){
e.printStackTrace();
}finally{
SQLServerDAOFactory.closeStatement(pstmt);
SQLServerDAOFactory.closeConnection(conn);
}
return tmp;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -