zhangjinbo@369
2016-11-03 12:49
采纳率: 54.5%
浏览 2.1k
已采纳

大神帮忙扫一眼呗,executeQuery和Statement的问题

 public class Login extends JFrame implements ActionListener{
//定义组件
JPanel jp1,jp2,jp3,jp4;//面板
JLabel jlb1,jlb2,jlb3;//标签
JButton jb1,jb2,jb3;//按钮
JTextField jtf;//文本
JPasswordField jpf;//密码
JRadioButton jrb1,jrb2=null;//
ButtonGroup bg=null;

//设定用户名和密码
static String userword;
static String password;

Connection ct=null;
Statement st=null;
ResultSet rs=null;




//构造函数
public Login(){
    //创建面板
    jp1=new JPanel();
    jp2=new JPanel();
    jp3=new JPanel();
    jp4=new JPanel();
    //创建标签
    jlb1=new JLabel("用户名");
    jlb2=new JLabel("密    码");
    jlb3=new JLabel("权    限:");

    //创建按钮
    jb1=new JButton("登录");
    jb2=new JButton("重置");
    jb3=new JButton("退出");


    jb1.addActionListener(this);
    jb2.addActionListener(this);
    jb3.addActionListener(this);
    jrb1=new JRadioButton("教师");
    jrb2=new JRadioButton("学生");
    bg=new ButtonGroup();
    bg.add(jrb1);
    bg.add(jrb2);
    jrb2.setSelected(true);



    //创建文本框
    jtf=new JTextField(10);
    //创建密码框
    jpf=new JPasswordField(10);

    //设置布局管理
    this.setLayout(new GridLayout(4, 1));//网格式布局

    //加入各个组件
    jp1.add(jlb1);
    jp1.add(jtf);

    jp2.add(jlb2);
    jp2.add(jpf);

    jp3.add(jlb3);
    jp3.add(jrb1);
    jp3.add(jrb2);

    jp4.add(jb1);
    jp4.add(jb2);
    jp4.add(jb3);

    //加入到JFrame
    this.add(jp1);
    this.add(jp2);
    this.add(jp3);
    this.add(jp4);


    //给窗口设置标题
    this.setTitle("学生成绩管理系统");
    //设置窗体大小
    this.setSize(300,200);
    //设置窗体初始位置
    this.setLocation(200, 150);
    //设置当关闭窗口时,保证JVM也退出
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //显示窗体
    this.setVisible(true);
    this.setResizable(true);

    //建立数据库
    try {
        Class.forName("org.gjt.mm.mysql.Driver");
        ct=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/studentmanager","root","123456");

    } catch (Exception e) {
        // TODO: handle exception
        System.out.println(e);
    }

}
public static void main(String[] args) {
new Login();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand()=="登录"){
    //如果选中教师登录
    if(jrb1.isSelected())
    {
         //创建火箭车  
        try { 
            st=ct.createStatement();

上面那句有错createStatement红线
Type mismatch: cannot convert from java.sql.Statement to java.beans.Statement

            rs=s.executeQuery("select * from info where quanxian = '老师'");

有错s.executeQuery 红线
The method executeQuery(String) is undefined for the type Statement

        //循环取出  
             while(rs.next()){  
                                userword=rs.getString(2);  
                 password=rs.getString(3);  
                 System.out.println("教师"+"\t"+userword+"\t"+password);      
             }
        } catch (SQLException e1) {  
            // TODO Auto-generated catch block  
            e1.printStackTrace();  
            System.out.println(e);
        }  


        //从数据库得到用户名和密码后调用登录方法,与输入的用户名和密码作比较
        tealogin();
    }else if(jrb2.isSelected()) //学生在登录系统
    {//http://blog.csdn.net/qq_25827845/article/details/50836362
         //创建火箭车  
        try { 
            st=ct.createStatement();

这里和上面一样

                        rs= st.executeQuery("select * from info where qunxian = '学生'");  
             //循环取出  
             while(rs.next()){  

                 userword=rs.getString(2);  
                 password=rs.getString(3);  
                 System.out.println("学生"+"\t"+userword+"\t"+password);      
             }
        } catch (SQLException e1) {  
            // TODO Auto-generated catch block  
            e1.printStackTrace();  
            System.out.println(e);
        }  
        //从数据库得到用户名和密码后调用登录方法,与输入的用户名和密码作比较
          stulogin();
    }
}

if(e.getActionCommand()=="重置"){
    clear();
}
if(e.getActionCommand()=="退出")
{
    System.exit(0);
}


}
//清空文本框和密码框
private void clear() {
// TODO Auto-generated method stub
jtf.setText("");
jpf.setText("");

}
//学生登录判断方法
private void stulogin() {
// TODO Auto-generated method stub
if(userword.equals(jtf.getText())&&password.equals(jpf.getText()))
{
//      System.out.println("登录成功");
    JOptionPane.showMessageDialog(null,"登录成功!","提示消息",JOptionPane.WARNING_MESSAGE);
    clear();
    //关闭当前界面
     dispose();
     //创建一个新界面
     UI ui=new UI();
}else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty())
{
    JOptionPane.showMessageDialog(null,"请输入用户名和密码!","提示消息",JOptionPane.WARNING_MESSAGE);
}else if(jtf.getText().isEmpty())
{
    JOptionPane.showMessageDialog(null,"请输入用户名!","提示消息",JOptionPane.WARNING_MESSAGE);
}else if(jpf.getText().isEmpty())
{
    JOptionPane.showMessageDialog(null,"请输入密码!","提示消息",JOptionPane.WARNING_MESSAGE);
}else
{
    JOptionPane.showMessageDialog(null,"用户名或者密码错误!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
    //清空输入框
    clear();
}

}
//教师登录判断方法
private void tealogin() {
// TODO Auto-generated method stub

if(userword.equals(jtf.getText())&&password.equals(jpf.getText()))
{
//      System.out.println("登录成功");
     JOptionPane.showMessageDialog(null,"登录成功!","提示消息",JOptionPane.WARNING_MESSAGE);
     clear();
    //关闭当前界面
     dispose();
     //创建一个新界面
     UI ui=new UI();
}else if(jtf.getText().isEmpty()&&jpf.getText().isEmpty())
{
    JOptionPane.showMessageDialog(null,"请输入用户名和密码!","提示消息",JOptionPane.WARNING_MESSAGE);
}else if(jtf.getText().isEmpty())
{
    JOptionPane.showMessageDialog(null,"请输入用户名!","提示消息",JOptionPane.WARNING_MESSAGE);
}else if(jpf.getText().isEmpty())
{
    JOptionPane.showMessageDialog(null,"请输入密码!","提示消息",JOptionPane.WARNING_MESSAGE);
}else
{
    JOptionPane.showMessageDialog(null,"用户名或者密码错误!\n请重新输入","提示消息",JOptionPane.ERROR_MESSAGE);
    //清空输入框
    clear();
}

}

}

就上面两个地方红线无法运行,求大神指点。
附上错误
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
Type mismatch: cannot convert from java.sql.Statement to java.beans.Statement
The method executeQuery(String) is undefined for the type Statement

at Login.actionPerformed(Login.java:174)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6216)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5981)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4583)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • qq_31258245 2016-11-03 13:21
    已采纳

    是不是你的jar包倒错了,检查以下

    评论
    解决 无用
    打赏 举报
查看更多回答(3条)

相关推荐 更多相似问题