进入登录界面后,需要对输入框和数据库进行连接,其中连接数据库的程序稍后列出,此处为框体内登录的代码:
Connection conn = null; //连接到数据库
java.sql.Statement st = null;
ResultSet rs = null;
try {
conn = coone.getConn();
st = conn.createStatement();
String user = urm.getText().trim();
String pwd = new String(pwd_password.getText()).trim();//将输入框的内容与数据库已有内容对应起来
String sql = "select*from user where admin='" + user
+ "'and password='" + pwd + "'";//从数据库的表中选取数据
rs = st.executeQuery(sql);
if (rs.next() && rs.getString("admin") != null) {
SaveUserInfo.setUserName(user);
SaveUserInfo.setPassword(pwd);
Main frame = new Main();
frame.setVisible(true);
Dao.this.setVisible(false);//账号密码正确,跳转至主界面
} else {
JOptionPane.showMessageDialog(this, "用户名或密码不正确!");//如账号密码不正确,则显出文本框“用户名或密码不正确!”
urm.setText("");
pwd_password.setText("");//清空账号密码输入框
urm.requestFocus();
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.getMessage(), "数据库异常",
JOptionPane.INFORMATION_MESSAGE);
} finally {
try {
conn.close();
} catch (Exception e) {
} }
连接数据库代码:
public class SaveUserInfo{
private static String admin="";
private static String password="";
public static String getPassword(){
return password;
}
public static void setPassword(String password){ 基于Java的图书管理信息系统设计(5):http://www.751com.cn/jisuanji/lunwen_17268.html