📄 commonuserdaoimpl.java
字号:
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class CommonUserDAOImpl implements CommonUserDAO {
//网络有问题或者查询到的名字不存在将会返回false,如果名字有重复返回true
public Connection getConnect() {
// TODO Auto-generated method stub
Context ctx = null;
Connection con = null;
try {
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/MySqlDS");
con = ds.getConnection();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public boolean selectUserForCheck(String username){
Connection conn = this.getConnect();
try {
PreparedStatement preproc = conn.prepareStatement("select userName from usertable where userName=?");
preproc.setString(1,username);
preproc.execute();
ResultSet rs = preproc.getResultSet();
if(rs.next()){
return true;
}else
return false;
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}finally{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -