Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql级别选择菜单的java返回变量_Java_Mysql_Jdbc_Dao_Getter Setter - Fatal编程技术网

mysql级别选择菜单的java返回变量

mysql级别选择菜单的java返回变量,java,mysql,jdbc,dao,getter-setter,Java,Mysql,Jdbc,Dao,Getter Setter,: 我想问你为什么它不起作用 我有一个叫做调用映射的过程 SELECT map FROM level WHERE level.id = map_choice 在我的java代码中:我试图为“级别选择”设置一个选择菜单 现在应该读取map选项并将map_选项设置为1,数据库应该识别我请求获取ID=1的map,只要它返回1 但结果是: Champ'map_choice'在子句中未知 这不正常?我仍然会犯错误不知道如何以及为什么 我的阅读数据库代码: package dao; 导入java.sql

:

我想问你为什么它不起作用 我有一个叫做调用映射的过程

SELECT map
FROM level
WHERE level.id = map_choice
在我的java代码中:我试图为“级别选择”设置一个选择菜单

现在应该读取map选项并将map_选项设置为1,数据库应该识别我请求获取ID=1的map,只要它返回1

但结果是:

Champ'map_choice'在子句中未知

这不正常?我仍然会犯错误不知道如何以及为什么

我的阅读数据库代码:

package dao;
导入java.sql.*

公共级罗兰岛{

private static  String URL = "jdbc:mysql://localhost/lorann? autoReconnect=true&useSSL=false&useUnicode=true&useJDBC"
            + "CompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
private static String LOGIN = "root";
private static String PASSWORD = "";

public Connection connection;
public Statement statement;



public static String getMAPQuery(int map_choice) {
     return "call CALL_MAP(" + map_choice + ");";
}


public LorannDAO () {

this.connection = null;
this.statement = null; 

}

public boolean open () {

    System.out.println("opening a connection");

    try {
    Class.forName("com.mysql.cj.jdbc.Driver");
    this.connection = DriverManager.getConnection(LorannDAO.URL,LorannDAO.LOGIN, LorannDAO.PASSWORD);

    this.statement = this.connection.createStatement();

    } catch (final ClassNotFoundException e) {
    e.printStackTrace();
    return false;
    } catch (final SQLException e) {
    e.printStackTrace();
    return false;
    }
    return true;
}


public void close () {

    System.out.println("closing a connection");

    if ( connection != null )
    try 
    {
    connection.close();
    } 
    catch ( SQLException ignore ) 
    {
    }
}


public String getMAp (int map_choice) throws SQLException {

final ResultSet resultSet = this.executeQuery(getMAPQuery(map_choice));

String map = ""; 

if (resultSet.first()) {
    map = resultSet.getString("map");
}

 return map;

}

private ResultSet executeQuery (String query_p) throws SQLException{

    ResultSet retur = this.statement.executeQuery(query_p);

    return retur;

}
}
还有我试图从何处获取选项ID的代码:

private int map_choice ;

/**
 * Instantiates a new model facade.
 * @throws IOException 
 */
public ModelFacade() throws IOException, SQLException 
{
    super();
    this.DAO = new LorannDAO();
    this.DAO.open();
    this.Map = new map(this.DAO.getMAp(map_choice));
    this.DAO.close();
}

public void connection () 
{
    this.DAO.open();
    try {
        this.Map.setMap(this.DAO.getMAp(map_choice));
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    this.DAO.close();
}

public map getMap() 
{
    return Map;
}

public void setMap(map map)
{
    Map = map;
}

@Override
public int getMap_choice() 
{
    return map_choice;
}

@Override
public void setMap_choice(int map_choice) 
{
    this.map_choice = map_choice;
}

对于您的代码,我有两个建议:

  • 您需要在
    开关盒
    块中添加
    中断
    ,否则程序将无法按预期工作

  • 不要将变量命名为
    map\u choice
    ,而是使用
    map choice


  • 修改完成后:我收到以下错误:java.lang.String无法在main.main.main(main.java:35)处的ControllerFacade.start(ControllerFacade.java:80)处转换为java.lang.Integer@RayanCh为
    ControllerFacade
    @RayanCh添加带有行号的代码嘿,你需要添加带有行号的代码,否则我们无法找到导致异常的行!你发布的代码格式不正确!
    private int map_choice ;
    
    /**
     * Instantiates a new model facade.
     * @throws IOException 
     */
    public ModelFacade() throws IOException, SQLException 
    {
        super();
        this.DAO = new LorannDAO();
        this.DAO.open();
        this.Map = new map(this.DAO.getMAp(map_choice));
        this.DAO.close();
    }
    
    public void connection () 
    {
        this.DAO.open();
        try {
            this.Map.setMap(this.DAO.getMAp(map_choice));
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        this.DAO.close();
    }
    
    public map getMap() 
    {
        return Map;
    }
    
    public void setMap(map map)
    {
        Map = map;
    }
    
    @Override
    public int getMap_choice() 
    {
        return map_choice;
    }
    
    @Override
    public void setMap_choice(int map_choice) 
    {
        this.map_choice = map_choice;
    }