程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> java-大神幫忙掃一眼呗,executeQuery和Statement的問題

java-大神幫忙掃一眼呗,executeQuery和Statement的問題

編輯:編程解疑
大神幫忙掃一眼呗,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)

最佳回答:


是不是你的jar包倒錯了,檢查以下

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved