JAVA中JDBC链接sql server之登录界面,做了一个图形的登录界面,通过调用数据库的unpw表检查用户是否可以登录。下图是数据库中unpw表相关属性然后运行,等一次成功了,再运行就是很多很多的警告。不知道哪里出错了,大神们,可以帮我看看么?源代码如下:package student1;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
//登录界面
public class Entry extends JFrame implements ActionListener{
int width = 400; // 窗体的宽度
int height = 220;// 高度
Connection con = null;// conn获取链接
Statement stmt = null;// 通过链接创建Statement对象stmt,在通过stmt对象执行sql语句
ResultSet rs = null;// 定义rs接收executeQuery()方法返回的ResultSet对象,查询的数据就存放在rs对象中
int qxian = 0;// 获取序号
// 最大的面板
JPanel pan = new JPanel();
// 系统登录标题面板
JPanel title_pan = new JPanel();
JLabel title_lab = new JLabel("学生信息管理系统登录", SwingConstants.CENTER);
// 放按钮的面板
JPanel button_pan = new JPanel();
JButton btnLogin = new JButton("登录");
JButton cancel_btn = new JButton("取消");
// 放置用户名和密码,角色的面板
JPanel content_pan = new JPanel();
JPanel username_pan = new JPanel();
JPanel password_pan = new JPanel();
JPanel role_pan = new JPanel();
JLabel username_lab = new JLabel("用户名:", SwingConstants.RIGHT);
JLabel passowrd_lab = new JLabel("密码: ", SwingConstants.RIGHT);
JLabel role = new JLabel("角色: ", SwingConstants.RIGHT);
JComboBox cbrole = new JComboBox();// 制作下拉列表
JTextField username = new JTextField();// 接收用户名的文本框
JPasswordField password = new JPasswordField();// 接收密码的密码框
public Entry() {
super("登陆");
init();
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void init() {
pan.setLayout(new BorderLayout());
title_lab.setFont(new Font("黑体", Font.BOLD, 20));
btnLogin.addActionListener(this);// 为登录,取消注册事件监听器
cancel_btn.addActionListener(this);
// 采用三行一列的表格布局
content_pan.setLayout(new GridLayout(3, 1));
cbrole.addItem("管理员");
cbrole.addItem("学生");// 下拉列表添加,学生,管理员角色
username.setColumns(6);
password.setColumns(6);
role_pan.add(role);
role_pan.add(cbrole);
username_pan.add(username_lab);
username_pan.add(username);
password_pan.add(passowrd_lab);
password_pan.add(password);
content_pan.add(username_pan);
content_pan.add(password_pan);
content_pan.add(role_pan);
button_pan.add(btnLogin);
button_pan.add(cancel_btn);
title_pan.add(title_lab);
pan.add(title_pan, BorderLayout.NORTH);
pan.add(button_pan, BorderLayout.SOUTH);
pan.add(content_pan, BorderLayout.CENTER);
this.add(pan);
this.setSize(width, height);
int screen_width = (int) this.getToolkit().getScreenSize().getWidth(); // 得到屏幕的宽度
int screen_height = (int) this.getToolkit().getScreenSize().getHeight(); // 得到屏幕的高度
this.setLocation((screen_width - width) / 2, (screen_height - height) / 2);
this.setResizable(false);
this.setVisible(true);
}
public void connDB() { // 连接数据库
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1434;DatabaseName=DBstudent", "sa",
"1234567890");
stmt = con.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void closeDB() // 关闭连接
{
try {
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object source = e.getSource();
String un = null;
String pw = null;
int qx = 0;
boolean success = false;// 用于判断是否登录成功
if (source == btnLogin) {
if (username.getText().equals("") || password.getText().equals("")) {// 判断是否输入了用户名和密码
JOptionPane.showMessageDialog(null, "登录名和密码不能为空!");
} else {
this.connDB();
try {
rs = stmt.executeQuery("select * from unpw");
while (rs.next()) {
un = rs.getString("un").trim();
pw = rs.getString("pw").trim();
qx = rs.getInt("qx");
if (username.getText().equals(un)) {
if (password.getText().equals(pw)) {
// actionCode = OK;
this.setVisible(false); // 隐藏登录窗体
qxian = cbrole.getSelectedIndex();
if (qxian == 0) {
JOptionPane.showConfirmDialog(null, "您点击了管理员界面", "提示信息", JOptionPane.DEFAULT_OPTION,
JOptionPane.OK_OPTION);
}
if (qxian == 1) {
JOptionPane.showConfirmDialog(null, "进入学生界面", "提示信息", JOptionPane.DEFAULT_OPTION,
JOptionPane.OK_OPTION);
}
success = true;
break;
} else {
JOptionPane.showMessageDialog(null, "密码错误!");
password.setText("");
success = true;
}
}
}
if (!success) {
JOptionPane.showMessageDialog(null, "登录名错误!");
username.setText("");
password.setText("");
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
/*
* 重置那些框 } else if (source == btncz) { txtUser.setText("");
* txtPwd.setText("");
*/
} else if (source == cancel_btn) {
System.exit(0);
}
}
}
添加回答
举报
0/150
提交
取消