📄 mobiledaoimpl.java
字号:
package com.magic.mobile.dao.impl;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.magic.mobile.dao.MobileDAO;
import com.magic.mobile.exception.NotFoundMobileException;
import com.magic.mobile.util.DBUtil;
import com.magic.mobile.vo.Mobile;
public class MobileDAOImpl implements MobileDAO {
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "insert into tmobile values(?,?,?,?)";
public boolean AddCode(String type, long startCode, long endCode) {
boolean flag = false;
conn = DBUtil.getConn();
pstmt = DBUtil.getpStmt(conn, sql);
try {
while (startCode <= endCode) {
String str = String.valueOf(startCode);
// if(str.length()==13){
pstmt.setString(1, str);
startCode++;
pstmt.setString(2, type);
pstmt.setString(3, "1111111111111");
pstmt.setString(4, "Y");
pstmt.addBatch();
}
// 关闭自动提交
conn.setAutoCommit(false);
try {
// 执行批处理
pstmt.executeBatch();
conn.commit();
} catch (RuntimeException e) {
e.printStackTrace();
}
// 将自动提交功能恢复到原来的状态
conn.setAutoCommit(true);
flag = true;
} catch (SQLException e) {
e.printStackTrace();
try {
conn.setAutoCommit(true);
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
} finally {
DBUtil.closeStmt(pstmt);
DBUtil.closeConn(conn);
}
return flag;
}
public boolean AddCodeFile(String FileUrl) {
Connection conn = null;
PreparedStatement pstmt = null;
String record = null;
int recCount = 0;
String[] strMobile= new String[3];
try {
FileReader fr = new FileReader(FileUrl);
BufferedReader br = new BufferedReader(fr);
while ((record = br.readLine()) != null) {
System.out.println(record);
strMobile[recCount++]=record;
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String sql = "insert into tmobile values(?,?,?,?)";
String type=strMobile[0];
long startCode=Long.parseLong(strMobile[1]);
long endCode=Long.parseLong(strMobile[2]);
conn = DBUtil.getConn();
pstmt = DBUtil.getpStmt(conn, sql);
try {
while (startCode <= endCode) {
String str = String.valueOf(startCode);
pstmt.setString(1, str);
startCode++;
pstmt.setString(2, type);
pstmt.setString(3, "1111111111111");
pstmt.setString(4, "Y");
pstmt.addBatch();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.setAutoCommit(false);
pstmt.executeBatch();
conn.commit();
conn.setAutoCommit(true);
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}finally {
DBUtil.closeStmt(pstmt);
DBUtil.closeConn(conn);
}
}
//查询用户
public Mobile selectMobile(Mobile mobile) throws NotFoundMobileException {
Connection conn = null;
ResultSet rs = null;
String sql = "select * from tmobile where mobile_number='"
+ mobile.getMobile_Number() + "' and is_available ='Y'";
try {
conn = DBUtil.getConn();
rs = DBUtil.getRs(conn, sql);
if(rs.next()){
//用户存在并且可用
mobile.setMobile_Number(rs.getString(1));
mobile.setMobile_Type(rs.getString(2));
mobile.setCard_Number(rs.getString(3));
mobile.setIs_Available(rs.getString(4));
}else{
//用户不存在或不可用
throw new NotFoundMobileException();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeRs(rs);
DBUtil.closeConn(conn);
}
return mobile;
}
//更新设置状态为不可用
public boolean updateMobile(Mobile mobile){
Connection conn = null;
PreparedStatement pstmt = null;
String sql ="update tmobile set is_available='N' where mobile_number=?";
try {
conn = DBUtil.getConn();
pstmt = DBUtil.getpStmt(conn, sql);
pstmt.setString(1,mobile.getMobile_Number());
pstmt.executeUpdate();
return true;
} catch (SQLException e) {
return false;
} finally {
DBUtil.closeStmt(pstmt);
DBUtil.closeConn(conn);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -