求助,登陆界面JDBC问题
import javax.swing.*; import java.awt.event.*;
import java.sql.*;
编译是能通过的,但是按“确定”键没反应。好像没有能成功连接数据库,请大家帮忙看看,修改一下。
class LoginFrm extends JFrame implements ActionListener
{
JLabel lbl1=new JLabel("用户名");
JLabel lbl2=new JLabel("密码");
JTextField txt=new JTextField(15);
JPasswordField pf=new JPasswordField();
JButton btn1=new JButton("确定");
JButton btn2=new JButton("取消");
public LoginFrm()
{
this.setTitle("登陆");
JPanel jp=(JPanel)this.getContentPane();
jp.setLayout(new GridLayout(3,2,10,10));
jp.add(lbl1);
jp.add(txt);
jp.add(lbl2);
jp.add(pf);
jp.add(btn1);
jp.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==btn1)
{
try
{
new sun.jdbc.odbc.JdbcOdbcDriver();
Connection con=DriverManager.getConnection("77AA95","sa","95");
Statement cmd=con.createStatement();
ResultSet rs=cmd.executeQuery("select id,password from web_user where id='"+txt.getText()+"' and password='"+pf.getText()+"'");
if(rs.next())
{
JOptionPane.showMessageDialog(null,"登陆成功!");
}
else JOptionPane.showMessageDialog(null,"用户名或密码错误!");
} catch(Exception ex){}
}
if(ae.getSource()==btn2)
{
txt.setText("");
pf.setText("");
}
}
public static void main(String arg[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
LoginFrm frm=new LoginFrm();
frm.setSize(750,200);
frm.setVisible(true);
}
}