日期:2014-05-20  浏览次数:20770 次

if (rs.getString(3)=="管理员")这个地方为什么不能比较
package per1;
import java.awt.*;

import java.sql.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.*;
public class Cland extends JFrame implements ActionListener{
/**
 * 
 */

JLabel jl1,jl2;
JTextField jtf1;
JPasswordField jpw1;
JPanel jp1,jp2,jp3,jp4;
JButton jb1,jb2;
JRadioButton jrb1,jrb2,jrb3;
ButtonGroup bg;
public static void main(String []args)
{
Cland cl=new Cland();
}
public Cland()
{
 jp1=new JPanel();
 jp2=new JPanel();
 jp3=new JPanel();
 jp4=new JPanel();
 jl1=new JLabel("用户名:");
 jl2=new JLabel("密     码:");

jtf1=new JTextField(10);
jpw1=new JPasswordField(10);
jb1=new JButton("登陆");
jb1.addActionListener(this);
jb2=new JButton("注销");
jrb1=new JRadioButton("管理员");
jrb2=new JRadioButton("老师");
jrb3=new JRadioButton("学生");
ButtonGroup bg=new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
bg.add(jrb3);
this.setLayout(new GridLayout(4,1));

jp1.add(jl1);
jp1.add(jtf1);
jp2.add(jl2);
jp2.add(jpw1);
jp3.add(jrb1);
jp3.add(jrb2);
jp3.add(jrb3);
jp4.add(jb1);
jp4.add(jb2);

this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(jp4);
this.setTitle("学生成绩管理系统");
this.setSize(300,150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);

}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb1)
{
 Connection conn=null;
   PreparedStatement ps=null;
ResultSet rs=null;

String []a=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:per","sa","sa");
ps=conn.prepareStatement("select * from user1 where 用户名=? and 密码=?");

ps.setString(1, jtf1.getText());
ps.setString(2, new String(jpw1.getPassword()));
rs=ps.executeQuery();

while(rs.next())
{

if (rs.getString(3)=="管理员")
{

Tsel re=new Tsel();
this.dispose();
}
}
}catch(Exception e1)
{
e1.printStackTrace();
}finally
{
try{

if(rs!=null)
{
rs.close();
}
if(ps!=null)ps.close();
if(conn!=null)conn.close();
}catch(Exception e2)
{
e2.printStackTrace();
}
}
}

}

}

------解决方案--------------------
改为
rs.getString(3).equals("管理员")
------解决方案--------------------
Java 里面String类型的比较是否相等用equals()
楼上正解!