java - mysql级别选择菜单的java返回变量
问题描述
我想问你为什么它不起作用我有一个名为 CALL_MAP 的程序
SELECT map
FROM level
WHERE level.id = map_choice
在我的 java 代码中:我正在尝试为“级别选择”添加一个选择菜单
else if (this.getView().getHome().getPanEdit().getButton_choice_2_home().getChoice() == 2) {
int map_choice = (int) JOptionPane.showInputDialog(null, "Choose a Map!", "Lorann-MapSelector",
JOptionPane.QUESTION_MESSAGE, null, LEVEL_LIST, LEVEL_LIST[0]);
if (map_choice > 5) {
System.exit(0);
}
/*Here is our switch with its choices*/
switch (map_choice) {
case 1:
ControllerFacade.this.map_choice = 1;
case 2:
ControllerFacade.this.map_choice = 2;
case 3:
ControllerFacade.this.map_choice = 3;
case 4:
ControllerFacade.this.map_choice = 4;
case 5:
ControllerFacade.this.map_choice = 5;
//Default, its an error
default:
ControllerFacade.this.map_choice = 4;
}
现在应该读取地图选择并将 map_choice 设置为 1,并且数据库应该认识到我要求获取 ID = 1 的地图,只要它返回 1
但结果是:
子句中未知的冠军“map_choice”
这不正常吗?我仍然收到错误,不知道如何以及为什么?
我的阅读数据库代码:
package dao;
导入java.sql.*;
公共类 LorannDAO {
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;
}
解决方案
对于您的代码,我有两个建议:
您需要添加
break
您的switch case
块,否则您的程序将无法按预期工作。不要将变量命名为
map_choice
,mapChoice
而是 使用
推荐阅读
- javascript - 无法将侦听器添加到由另一个按钮创建的按钮
- swift - 如何快速将下载文件转发到我的应用程序?
- sql - 使用 SELECT 查询检查具有单个值的多个列
- c# - 如何打印具有属性的对象列表?
- c# - 如何在 C# 中使用 IText7 在现有 pdf 的顶部添加条形码?
- oracle - ORA-01873: 使用 EXTRACT 函数时区间的前导精度太小
- c++ - 数组值意外更改
- google-apps-script - 有没有办法在保留文本格式的同时替换 TextBox 形状内的文本?
- django-models - django 模型中使用的变量是类变量还是实例变量?
- scala - 将 Json 从蛇案例解析为骆驼案例