【问题标题】:Java ID verification error : connector errorJava ID 验证错误:连接器错误
【发布时间】:2020-05-17 16:29:10
【问题描述】:

我尝试检查标识符是否存在,但出现线路错误

st = Connecter.getConnection().prepareStatement(query)

出现错误:找不到符号 符号:方法prepareStatement(String) 位置:类对象

并在行中

catch (SQLException ex) {
            Logger.getLogger(Login_Form.class.getName()).log(Level.SEVERE, null, ex);

出现错误找不到符号 符号:类 SQLException 位置:类Login_Form

这是我的全部代码

PreparedStatement st;
        ResultSet rs;

        // donnez l'username et le password 
        String username = jTextField_Username.getText();
        String password = String.valueOf(jPasswordField.getPassword());

        // requete indiquant si les identifiants existent 
        String query = "SELECT * FROM 'users' WHERE 'username' = ? AND 'password' = ?";

        try {
            st = Connecter.getConnection().prepareStatement(query)

            st.setString(1, username);
            st.setString(2, password);
            rs = st.executeQuery();

            if(rs.next())

            {
                // Show my new form 
            }else{
                // Error message 
            }


        } catch (SQLException ex) {
            Logger.getLogger(Login_Form.class.getName()).log(Level.SEVERE, null, ex);
        }

我的连接器页面:

package javaapplicationhotel2;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.sql.*;

/**
 *
 * @author camil
 */




public class Connecter {

    static Object getConnection() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    Connection con; 



    public Connecter() {
      try{
      Class.forName("com.mysql.cj.jdbc.Driver");
      }catch(ClassNotFoundException e){
          System.err.println(e);


    }
    try{
    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel","root","");
    }catch(SQLException e){System.err.println(e);}
    }
    Connection obtenirconnexion(){return con;}

    PreparedStatement prepareStatement(String select__from_classe) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.



    }


}

你有解决办法吗?

【问题讨论】:

    标签: java mysql sql


    【解决方案1】:

    问题:

    Connecter::getConnection 没有返回 Connection 对象。

    解决方案:

    替换

    static Object getConnection() {
        throw new UnsupportedOperationException("Not supported yet."); // To change body of generated methods, choose Tools | Templates.
    }
    Connection con; 
    

    public static Connection getConnection() {
        return con;
    }
    static Connection con; 
    

    【讨论】:

    • ty,但我在“return con;”处遇到错误当我更改它时“不能从静态上下文引用非静态变量”
    • @MendosaHenry - 因为Connection con 没有被声明为staticstatic 方法不能直接引用非静态变量。我已经更新了答案。如果有任何进一步的问题/疑问,请随时发表评论。
    猜你喜欢
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 2015-05-01
    • 1970-01-01
    相关资源
    最近更新 更多