萌新求助(参照书做了个练习,结果登录后没反应,没跳转到主界面)
IT小喳喳 2018-07-17 10:15:18 DB类代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnector {
public static Connection conn=null; //Connection对象(链接)
//连接数据库
public static Connection getConn(){
try{
//加载注册MySQL的JDBC驱动
Class.forName("com.mysql.jdbc.Driver");
//编写链接字符串,创建并且获取链接
return conn;
}catch(ClassNotFoundException e){
e.printStackTrace();
}
String url ="jdbc:mysql://localhost:3306/studentmanage?useSSL=false";
String user ="root";
String PassWord = "513513";
try {
conn = DriverManager.getConnection(url,user,PassWord);
if (conn !=null) {
System.out.println("数据库连接成功!");
}
}catch(SQLException e){
e.printStackTrace();
}
return conn;
}
}
login类代码
import java.awt.*;
import java.awt.event.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.*;
import org.mhr.StudentManager.main.db.*;//引用已建立的main.db公共类
import org.mhr.StudentManager.student.management.StudentManage;
import org.mhr.StudentManager.teacher.management.TeacherManage;
public class Login extends Frame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel JLUserName = new JLabel("用户名:"); //创建“用户名:”标签对象
JLabel JLPaw = new JLabel("密 码:"); //创建“密码:”标签对象
JTextField JTUserName = new JTextField(); //创建输入用户名文本框对象
JPasswordField JPsw = new JPasswordField(); //创建输入密码文本框对象
//创建功能按钮
JButton JB1 = new JButton("登录"); //创建登录按钮对象
JButton JB2 = new JButton("取消"); //创建取消按钮对象
JLabel JL1 = new JLabel("身 份:"); //创建身份标签对象
JComboBox<String> JC = new JComboBox<String>(); //创建身份复选框对象
public Login()
{
this.setTitle("学生信息管理系统"); //设置窗口标题
this.setLayout(null); //设置窗口布局管理器
//标签“姓名”
JLUserName.setBounds(100, 40, 100, 20); //设置姓名标签起始位置
this.add(JLUserName); //将姓名标签组件添加到容器
JTUserName.setBounds(200, 40, 80, 20); //设置输入姓名文本框的起始位置
this.add(JTUserName); //将文本框组件添加到容器中
//标签“密码”
JLPaw.setBounds(100, 100, 60, 20); //设置密码标签起始位置
this.add(JLPaw); //将密码标签组件添加到容器中
JPsw.setBounds(200, 100, 80, 20); //设置输入密码的文本框其实位置
this.add(JPsw); //将输入密码文本框组件添加到容器中
//身份复选框标签
JL1.setBounds(100, 150,60,20); //设置身份标签的其实位置
this.add(JL1); //将身份标签组件添加到容器中
JC.setBounds(200, 150, 80, 20); //设置组合框的其实位置
this.add(JC); //将组合框组件添加到容器中
JC.addItem(new String ("学生")); //给组合框添加内容“学生”
JC.addItem(new String ("教师")); //给组合框添加内容“教师”
//登录按钮
JB1.setBounds(100, 200, 60, 20); //设置登录按钮的初始位置
this.add(JB1); //将登录按钮组件添加到容器中
JB1.addActionListener(this); //给按钮添加监听器
JB2.setBounds(200,200,60,20); //设置取消按钮的初始位置
this.add(JB2); //将取消按钮组件添加到容器中
JB2.addActionListener(this); //给取消按钮添加监听器
this.setVisible(true); //设置窗口可见性
this.setBounds(10, 10, 400, 250); //设置窗口尺寸大小
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
};
});
}
public static void main(String args[]) {
new Login(); //创建login对象
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == JB1) {
//将文本框中包含的文本传给字符串name
String name =JTUserName.getText();
String password = new String(JPsw.getPassword());
//将当前所选项传给字符串
String box = (String) JC.getSelectedItem();
String loginsql = null;
if(e.equals("学生")) { //选择学生身份登录
loginsql = "select password from students where name='"+name+"'";
if(logindb(password,loginsql))
new StudentManage(); //调用学生信息主窗口
}else if (box.equals("教师")) { //选择教师身份登录
loginsql = "select password from teachers where name='"+name+"'";
if (logindb(password,loginsql))
new TeacherManage(); //调用教师信息主窗口
}
}
}
private boolean logindb(String password, String sql) {
//查询数据库
ResultSet rs = null;
try {
rs = DBConnector.getConn().createStatement().executeQuery(sql);
}catch(Exception e1){
e1.printStackTrace();
}
try {
if(rs.next() && rs.getString(1).equals(password)) {
rs.close();
return true;
}
}catch(SQLException e1) {
e1.printStackTrace();
}
return false;
}
}