import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;class LoginFrm extends JFrame implements ActionListener// throws Exception
{
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
{
Class.forName("com.mysql.jdbc.Driver");//mysql数据库
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/cyimpression","root","");//数据库名为cyimpression,密码为空
Statement cmd=con.createStatement();
ResultSet rs=cmd.executeQuery("select * from  register where username='"+txt.getText()+"' and password2='"+pf.getText()+"'");//表名为register,username和password2是存放用户名和密码的字段名
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(400,200);
frm.setVisible(true);
}
}
输入数据库中包含的正确的用户名和密码后点击确定,没有任何反应。应该是连接数据库的问题,我之前做php连接mysql是可以用的,
是不是java连接还要装什么驱动,有没有不装的做法,有的话怎么修改,没有的话,具体怎么操作装驱动。
顺便帮忙看下还有其他什么问题没有。主要是连接mysql数据库方面。麻烦了,谢谢!

解决方案 »

  1.   

    下个连接mysql的驱动,一个jar包,复制到你工程的lib文件夹下
      

  2.   

    没导入驱动,自己上网下,mysql-connector-java-5.0.5-bin.jar!!!!!然后点击工程右键,点属性,java构建路径--库--添加外部JAR!
      

  3.   

    设置一下断点!!debug一下,看看哪里出问题了!
      

  4.   

    出了使用ODBC数据库不需要导第三方的驱动包,其他的好像都需要导
      

  5.   

    你的代码写的有误。我给你的代码除了数据库注释掉外,其他的也改变了下:
    用的是固定的用户名密码,但原理是一样的(不想再建表)。
    if(ae.getSource()==btn2)与if(ae.getSource()==btn1)是平等的不能写在里面
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.sql.*;public class LoginFrm extends JFrame implements ActionListener// throws Exception
    {
    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);//2按钮就接收鼠标事件
    btn2.addActionListener(this);
    }public void actionPerformed(ActionEvent ae)//事件发生机制
    {
    if(ae.getSource()==btn1)
    {
       /* try
    {
    Class.forName("com.mysql.jdbc.Driver");//mysql数据库
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost/cyimpression","root","");//数据库名为cyimpression,密码为空
    Statement cmd=con.createStatement();
    ResultSet rs=cmd.executeQuery("select * from  register where username='"+txt.getText()+"' and password2='"+pf.getText()+"'");//表名为register,username和password2是存放用户名和密码的字段名
    if(rs.next())
    {
    JOptionPane.showMessageDialog(null,"登陆成功!");
    }
    else
    JOptionPane.showMessageDialog(null,"用户名或密码错误!");
    } catch(Exception ex){}
      */ 
       String s1=txt.getText();
       String s2=pf.getText();
       if(s1.equals("admin")&&s2.equals("admin")){
       JOptionPane.showMessageDialog(null,"登陆成功!");
       }else{
    JOptionPane.showMessageDialog(null,"用户名或密码错误!");
                }
    }
    if(ae.getSource()==btn2)
    {
    txt.setText("");
    pf.setText("");
    }
    }public static void main(String arg[])
    {
    JFrame.setDefaultLookAndFeelDecorated(true);
    LoginFrm frm=new LoginFrm();
    frm.setSize(400,200);
    frm.setVisible(true);
    }
    }