?? userdao.java~5~
字號(hào):
package dao;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class UserDAO {
DataSource ds;
public UserDAO() {
try {
InitialContext ic = new InitialContext();
ds = (DataSource) ic.lookup("java:comp/env/jdbc/eBank");
} catch (NamingException ex) {
ex.printStackTrace();
}
}
public boolean addCustomer(String username, String password
) {
try {
Connection con = ds.getConnection();
String sql =
"insert into j2eeappuser values(?,?,seq_user.nextval)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
pstmt.executeUpdate();
pstmt.close();
con.close();
return true;
} catch (SQLException ex) {
ex.printStackTrace();
return false;
}
}
public boolean deleteCustomer(int id) {
try {
Connection con = ds.getConnection();
String sql = "delete from j2eeappuser where userid=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
pstmt.close();
con.close();
return true;
} catch (SQLException ex) {
ex.printStackTrace();
return false;
}
}
public boolean validate(String name, String password) {
try {
Connection con = ds.getConnection();
String sql =
"select * from j2eeappuser where username=? and password=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
boolean flag = false;
if (rs.next()) flag = true;
rs.close();
pstmt.close();
con.close();
return flag;
} catch (SQLException ex) {
ex.printStackTrace();
return false;
}
}
public int getUserid(String name, String password) {
try {
Connection con = ds.getConnection();
String sql =
"select * from j2eeappuser where username=? and password=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
rs.next();
int id = rs.getInt("userid");
rs.close();
pstmt.close();
con.close();
return id;
} catch (SQLException ex) {
ex.printStackTrace();
return 0;
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -