JAVA 登陆界面出异常,大家看看,
主函数是package systemmanger;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame
{
JFrame f;
private JLabel name=new JLabel("用 户 名:");
private JLabel psw=new JLabel("密 码:");
private JTextField tf1=new JTextField(10);
private JTextField tf2=new JTextField(10);
private JButton b1=new JButton("确定");
private JButton b2=new JButton("取消");
public Main() {
setLayout(new FlowLayout());
add(name);
add(tf1);
add(psw);
add(tf2);
add(b1);
b1.addActionListener(new actionthing());
add(b2);
b2.addActionListener(new actionthing());
setBounds(400,300,200,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private class actionthing implements ActionListener //throws Exception
{
public void actionPerformed (ActionEvent e)
{
UserInfor ui=null;
if(e.getSource() == b1)
{
String nameStr=tf1.getText();
String pwdStr=tf2.getText();
if(nameStr.equals("")||pwdStr.equals(""))
{
JOptionPane.showConfirmDialog(Main.this,"帐号或密码不能为空","错误",JOptionPane.OK_CANCEL_OPTION,JOptionPane.ERROR_MESSAGE);
}else
{
try
{
ui=new UserInfor();
}catch(Exception g) { System.out.println("数据库连结失败"); }
try
{
boolean temp=ui.isRight(nameStr,pwdStr);
ui.closeConnection();
if(temp)
{
System.out.println("密码有效");
}
}//catch(Exception g){ System.out.println("密码错误哦..");}
catch(Exception g){ g.printStackTrace();}
}
}
else if(e.getSource() ==b2)
{
tf1.setText("");
tf2.setText("");
}
}
}
public static void main(String[] args)
{
// TODO code application logic here
new Main();
}
}
连接数据库调用的类
package systemmanger;
import java.sql.*;
import java.util.*;
import org.omg.
public class UserInfor
{
private Connection con;
private String url="jdbc:microsoft:sqlserver://192.168.0.118:1433;DatabaseName=abc";
private String user="sa";
private String password="";
/** Creates a new instance of UserInfor */
public void UserInfor() throws ClassNotFoundException,SQLException,InstantiationException,IllegalAccessException
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
con = DriverManager.getConnection(url,user,password);
Statement sql=con.createStatement();
ResultSet rs= sql.executeQuery("select * from user2 ");
}
public boolean isRight(String name,String psd) throws Exception
{
String Select="select * from user2 where name = '"+name+"'&& password = '"+psd+"'";
Statement stm = con.createStatement();
ResultSet rst = stm.executeQuery(Select);
while(rst.next())
{
return true;
}
System.out.println("数据库测试成功。");
return false;
}
public void closeConnection()
{
try{
con.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
里面的user2 是数据库里面的表名 name 和 password 是表中的两个选取项。
函数里面的两个参数,是从登陆窗口传送过来的输入数据。
可是,当我每次输入数据后,一点确定按钮,编译器就会报。
java.lang.NullPointerException
这个异常,请问下, 这是什么原因啊
知道的说下.
谢谢了..