![]() |
#2
笨笨的树袋熊2016-06-12 11:55
|

package newpackage;
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import *;
public class denglu extends JFrame implements ActionListener {
Connection con;
ResultSet rs;
Statement st;
JTextField text1;
JPasswordField text2;
JButton button1, button2, button3;
CheckboxGroup quanxianbox = new CheckboxGroup();
Checkbox student = new Checkbox("学生", true, quanxianbox);
Checkbox teacher = new Checkbox("管理员", false, quanxianbox);
String useru = new String();
String passu = new String();
String quanxian = new String();
String quanxian1 = new String();
String ID = new String();
public denglu() {
text1 = new JTextField(10);
text2 = new JPasswordField(10);
button1 = new JButton("登陆");
button1.addActionListener(this);
button2 = new JButton("注册");
button2.addActionListener(this);
button3 = new JButton("退出");
button3.addActionListener(this);
Box box1 = Box.createHorizontalBox();
box1.add(new JLabel("欢迎使用本系统!", JLabel.CENTER));
Box box2 = Box.createHorizontalBox();
box2.add(new JLabel("制作人--fish、fang、xiaozhi、sword", JLabel.CENTER));
Box box3 = Box.createHorizontalBox();
box3.add(new JLabel("用户名:", JLabel.CENTER));
box3.add(text1);
Box box4 = Box.createHorizontalBox();
box4.add(new JLabel("密 码: ", JLabel.CENTER));
box4.add(text2);
Box box5 = Box.createHorizontalBox();
box5.add(student);
Box box6 = Box.createHorizontalBox();
box6.add(teacher);
Box boxH = Box.createVerticalBox();
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(box5);
boxH.add(box6);
boxH.add(Box.createVerticalGlue());
JPanel pCenter = new JPanel();
pCenter.add(boxH);
setLayout(new BorderLayout());
add(pCenter, BorderLayout.CENTER);
JPanel pSouth = new JPanel();
pSouth.add(button1);
pSouth.add(button2);
pSouth.add(button3);
add(pSouth, BorderLayout.SOUTH);
setVisible(true);
setBounds(100, 50, 300, 200);
validate();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
try {//连接数据库
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e2) {
System.out.println("" + e2);
}
try {
con = DriverManager.getConnection("jdbc:odbc:lhm", "", "");
st = con.createStatement();
rs = st.executeQuery("Select * FROM denglu");
} catch (SQLException el) {
}//连接数据库
String usershu = new String();
usershu = text1.getText();
ID = text1.getText();
String passshu = new String();
passshu = text2.getText();
try {
File classData = new File("C:\\User\\lenovo\\Desktop\\Course1\\data\\lhm.mdb");
DataOutputStream dout = new DataOutputStream(new FileOutputStream(classData));
dout.writeInt(Integer.parseInt(ID));
dout.close();
} catch (Exception eo) {
}
if (usershu.length() > 0) {
try {
int count = 0;
while (rs.next()) {
useru = rs.getString("userid");
passu = rs.getString("userpassword");
quanxian1 = rs.getString("userpower");
if ((usershu.equals(useru)) && (passshu.equals(passu))) {
++count;
if (teacher.getState()) {
quanxian = teacher.getLabel();
if (quanxian1.equals(quanxian)) {
guanliyuan gy = new guanliyuan();
gy.setBounds(200, 150, 100, 100);
gy.guanliyuan();
gy.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "权限错误,请选择正确权限进入!");
}
} else if (student.getState()) {
quanxian = student.getLabel();
if (quanxian1.equals(quanxian)) {
load1 ld = new load1();
ld.setBounds(200, 150, 100, 100);
ld.load1();
ld.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "权限错误,请选择正确权限进入!");
}
}
break;
}
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "用户名或密码错!");
text1.setText("");
text2.setText("");
}
} catch (Exception ee) {
}
} else {
JOptionPane.showMessageDialog(null, "请输入用户名!");
text1.setText("");
text2.setText("");
}
} else if (e.getSource() == button3) {
System.exit(0);
}
try//关闭数据库
{
st.close();
con.close();
} catch (SQLException e3) {
JOptionPane.showMessageDialog(null, "数据库关闭失败!");
}
}
public static void main(String args[]) {
new denglu();
}
}
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import *;
public class denglu extends JFrame implements ActionListener {
Connection con;
ResultSet rs;
Statement st;
JTextField text1;
JPasswordField text2;
JButton button1, button2, button3;
CheckboxGroup quanxianbox = new CheckboxGroup();
Checkbox student = new Checkbox("学生", true, quanxianbox);
Checkbox teacher = new Checkbox("管理员", false, quanxianbox);
String useru = new String();
String passu = new String();
String quanxian = new String();
String quanxian1 = new String();
String ID = new String();
public denglu() {
text1 = new JTextField(10);
text2 = new JPasswordField(10);
button1 = new JButton("登陆");
button1.addActionListener(this);
button2 = new JButton("注册");
button2.addActionListener(this);
button3 = new JButton("退出");
button3.addActionListener(this);
Box box1 = Box.createHorizontalBox();
box1.add(new JLabel("欢迎使用本系统!", JLabel.CENTER));
Box box2 = Box.createHorizontalBox();
box2.add(new JLabel("制作人--fish、fang、xiaozhi、sword", JLabel.CENTER));
Box box3 = Box.createHorizontalBox();
box3.add(new JLabel("用户名:", JLabel.CENTER));
box3.add(text1);
Box box4 = Box.createHorizontalBox();
box4.add(new JLabel("密 码: ", JLabel.CENTER));
box4.add(text2);
Box box5 = Box.createHorizontalBox();
box5.add(student);
Box box6 = Box.createHorizontalBox();
box6.add(teacher);
Box boxH = Box.createVerticalBox();
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(box5);
boxH.add(box6);
boxH.add(Box.createVerticalGlue());
JPanel pCenter = new JPanel();
pCenter.add(boxH);
setLayout(new BorderLayout());
add(pCenter, BorderLayout.CENTER);
JPanel pSouth = new JPanel();
pSouth.add(button1);
pSouth.add(button2);
pSouth.add(button3);
add(pSouth, BorderLayout.SOUTH);
setVisible(true);
setBounds(100, 50, 300, 200);
validate();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
try {//连接数据库
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e2) {
System.out.println("" + e2);
}
try {
con = DriverManager.getConnection("jdbc:odbc:lhm", "", "");
st = con.createStatement();
rs = st.executeQuery("Select * FROM denglu");
} catch (SQLException el) {
}//连接数据库
String usershu = new String();
usershu = text1.getText();
ID = text1.getText();
String passshu = new String();
passshu = text2.getText();
try {
File classData = new File("C:\\User\\lenovo\\Desktop\\Course1\\data\\lhm.mdb");
DataOutputStream dout = new DataOutputStream(new FileOutputStream(classData));
dout.writeInt(Integer.parseInt(ID));
dout.close();
} catch (Exception eo) {
}
if (usershu.length() > 0) {
try {
int count = 0;
while (rs.next()) {
useru = rs.getString("userid");
passu = rs.getString("userpassword");
quanxian1 = rs.getString("userpower");
if ((usershu.equals(useru)) && (passshu.equals(passu))) {
++count;
if (teacher.getState()) {
quanxian = teacher.getLabel();
if (quanxian1.equals(quanxian)) {
guanliyuan gy = new guanliyuan();
gy.setBounds(200, 150, 100, 100);
gy.guanliyuan();
gy.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "权限错误,请选择正确权限进入!");
}
} else if (student.getState()) {
quanxian = student.getLabel();
if (quanxian1.equals(quanxian)) {
load1 ld = new load1();
ld.setBounds(200, 150, 100, 100);
ld.load1();
ld.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "权限错误,请选择正确权限进入!");
}
}
break;
}
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "用户名或密码错!");
text1.setText("");
text2.setText("");
}
} catch (Exception ee) {
}
} else {
JOptionPane.showMessageDialog(null, "请输入用户名!");
text1.setText("");
text2.setText("");
}
} else if (e.getSource() == button3) {
System.exit(0);
}
try//关闭数据库
{
st.close();
con.close();
} catch (SQLException e3) {
JOptionPane.showMessageDialog(null, "数据库关闭失败!");
}
}
public static void main(String args[]) {
new denglu();
}
}
新手确实不知道哪里错了,只知道错误是空指针异常