写了个java操作数据库来验证用户登录,数据库有两个用户第一个是admin密码是123456,第二个是user密码是123456
为什么我点登录,admin出错,user能正常登录。我想是在ResultSet rs的操作有问题,不知道怎么做,顺便问一下Button的字体汉字怎么正常显示,写汉字就变成乱码了.下面是代码
package test_swing;
import java.awt.*;
import java.sql.*;import javax.swing.*;
import java.awt.event.*;
import java.awt.peer.ButtonPeer;
import java.sql.ResultSet;
public class TestFrame { Frame f=new Frame("测试框架");
TextField userName=new TextField(15);
TextField password=new TextField(10);
Button b=new Button("login");
Button c=new Button("cancle");
Panel p=new Panel();
public void TestFrame() 
{
// TODO Auto-generated method stub
ConDatabase cct=new ConDatabase();
cct.Conn();
f.add(p);
f.setSize(400,400);
f.setVisible(true);
f.add(new Container());
//p.setLayout(new BorderLayout());
    p.add(userName,BorderLayout.SOUTH);
    p.add(b,BorderLayout.WEST);
    p.add(password,BorderLayout.NORTH);
    p.add(c,BorderLayout.WEST);
    b.addActionListener(new LoginListener());
    c.addActionListener(new CancleListener());
f.addWindowListener(new MyWindowListener());
}
class ConDatabase
{
private String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=sample";
private ResultSet rs=null;
    private String user="sa";
    private String pw="sa";
    private Statement stmt=null;
void Conn()
{
try {
//Class.forName("org.gjt.mm.mysql.Driver");//mysql驱动
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
//System.out.println("数据库驱动程序注册成功") ;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
boolean GetCon(){
try {
Connection conn=DriverManager.getConnection(url,user,pw);
//System.out.println("Connection Successful!"); 
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery("select count(*) from userInfo where name="+userName.getText()+" and password="+password.getText());
do{
if(rs == null)
{
//rs.next();
return true;
}else{

return false;
}
}
while(rs.next());

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
class MyWindowListener implements WindowListener
{
public void windowClosing(WindowEvent e)
{
e.getWindow().setVisible(false);
((Window)e.getComponent()).dispose();
System.exit(0);
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
}
class LoginListener implements ActionListener
{ @Override
public void actionPerformed(ActionEvent e) 
{
// TODO Auto-generated method stub
 //password.setText(userName.getText());
ConDatabase cc=new ConDatabase();
if(cc.GetCon())
{
JOptionPane.showMessageDialog(null, "Login Sucessful!");
}
else
{
JOptionPane.showMessageDialog(null, "Name or Password is wrong!");
}
}
}
class CancleListener implements ActionListener
{ @Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
password.setText("");
}
}
public static void main(String[] args)
{

TestFrame tf=new TestFrame();
tf.TestFrame();
}
}

解决方案 »

  1.   

    ("select count(*) from userInfo where name="+userName.getText()+" and password="+password.getText());
    改为:
    ("select count(*) from userInfo where name='"+userName.getText()+"' and password='"+password.getText()+"'");正常字符串都用用单引号括住吧
      

  2.   

    验证成功了,在eclipse中登录都行,但是我用Jsmooth生成exe文件后运行,登录不上了
    还有button上的汉字显示怎么没弄?