gpt4 book ai didi

java - 为什么 select 语句总是返回最后插入的值?

转载 作者:太空宇宙 更新时间:2023-11-04 12:46:22 24 4
gpt4 key购买 nike

当我尝试使用准备好的语句选择记录时,它总是给出我最近添加的最后插入的值。

首先我做的是在第一个表中搜索记录。如果记录存在,外键表将填充值。我的主键和外键表运行良好。这些值适本地填充到相应的组件中,但它没有给我正确的值。有什么帮助吗?

1st Table

这是引用第二个表的外键表的主键表。

2nd Table

选择查询:

String searchSECTIONNAME = "SELECT * FROM allsections_list WHERE SECTION_NAME = ?";//1st Select Statement

String searchSECTIONSETTINGS = "SELECT allsections_list.`SECTION_ID`, allsections_settings.ADVISER_ASSIGNED, allsections_settings.SECTION_POPULIMIT,\n" +
"allsections_settings.ROOM_ASSGN, allsections_settings.YRLEVEL_ASSGN, allsections_settings.SCHOOL_YEAR, allsections_settings.SESSION_ASSIGNED\n" +
"FROM allsections_list\n" +
"RIGHT JOIN allsections_settings\n" +
"ON allsections_list.`SECTION_ID`=allsections_settings.`SECTION_ID`";//2nd Select Statement

所以我在这里所做的是使用右连接SECTION_NAME列连接到外键表。如果记录存在,它将连接两个表。

代码:

 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
String searchSection = Section_SearchSection_Textfield.getText();


try (Connection myConn = DBUtil.connect();
PreparedStatement myFirstPs = myConn.prepareStatement(searchSECTIONNAME);)
{
myFirstPs.setString(1, searchSection);

try (ResultSet myFirstRs = myFirstPs.executeQuery())
{
int resultCounter = 0;
while (myFirstRs.next())
{
String mySectionName = myFirstRs.getString(2);//Get the value of SECTION_NAME
Section_SectionName_TextField.setText(mySectionName);
Section_SectionName_TextField.setEnabled(true);

try (PreparedStatement mySecondPs = myConn.prepareStatement(searchSECTIONSETTINGS))
{
try (ResultSet mySecondRs = mySecondPs.executeQuery())
{
while (mySecondRs.next())
{
String myAdviserAssigned = mySecondRs.getString(2);
Section_Student_Limit_ComboBox1.setSelectedItem(myAdviserAssigned);
Section_Student_Limit_ComboBox1.setEnabled(true);

String mySectionPopulation = mySecondRs.getString(3);
Section_Student_Limit_ComboBox.setSelectedItem(mySectionPopulation);
Section_Student_Limit_ComboBox.setEnabled(true);

String myRoomAssigned = mySecondRs.getString(4);
Section_Room_Assignment_ComboBox.setSelectedItem(myRoomAssigned);
Section_Room_Assignment_ComboBox.setEnabled(true);

String myYearLevelAssigned = mySecondRs.getString(5);
Section_Session_Level_ComboBox.setSelectedItem(myYearLevelAssigned);
Section_Session_Level_ComboBox.setEnabled(true);

String mySchoolYear = mySecondRs.getString(6);
Section_SchooYear_ComboBox.setSelectedItem(mySchoolYear);
Section_SchooYear_ComboBox.setEnabled(true);

String mySessionAssigned = mySecondRs.getString(7);
Section_Session_Settings_ComboBox.setSelectedItem(mySessionAssigned);
Section_Session_Settings_ComboBox.setEnabled(true);

resultCounter++;

}//end of loop mySecondRs (ResultSet)
}//end of try mySecondRs (ResultSet)
}//end of try mySecondPs (PreparedStatement)

}//end of loop myFirstRs (ResultSet)
if (resultCounter == 1)//If exist
{
JOptionPane.showMessageDialog(null, "Data Found");
}
else//If not exist
JOptionPane.showMessageDialog(null, "No Data Found");
}//end of try myFirstRs (ResultSet)

}//end of try myFirstPs (PreparedStatement)

catch (SQLException e)
{
DBUtil.processException(e);
}//end of catch
}

正如你在这里看到的。在我的第一个 ResultSet myFirstRs 中,当我搜索现有 SECTION_NAME 时,将填充外键值。如果我的循环中有什么东西纠正了我。提前致谢!

更新!

我在第二个选择查询中添加了 ORDER BY 子句。因为如果没有这个,数据库将返回它想要的内容,所以我所做的就是修改查询并添加 ORDER BY 子句,如下所示:

String searchSECTIONSETTINGS = "SELECT allsections_list.`SECTION_ID`, allsections_settings.ADVISER_ASSIGNED, allsections_settings.SECTION_POPULIMIT,\n" +
"allsections_settings.ROOM_ASSGN, allsections_settings.YRLEVEL_ASSGN, allsections_settings.SCHOOL_YEAR, allsections_settings.SESSION_ASSIGNED\n" +
"FROM allsections_list\n" +
"RIGHT JOIN allsections_settings\n" +
"ON allsections_list.`SECTION_ID` = allsections_settings.`SECTION_ID`" +
"ORDER BY allsections_list.SECTION_ID";

当我运行项目时仍然给我错误的值。我尝试在 NetBeans 查询中运行此查询,并按 ASC 顺序给我一个值。

enter image description here

最佳答案

我刚刚找到了一个最简单的解决方案。我使用右连接连接了两个表。

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
String searchSection = Section_SearchSection_Textfield.getText().replace("!", "!!").replace("%", "!%").replace("_", "!_")
.replace("[", "![");

String searchSECTIONSETTINGS = "SELECT allsections_list.SECTION_ID as 'ID', allsections_list.SECTION_NAME as 'Section Name', allsections_settings.ADVISER_ASSIGNED as 'Adviser', allsections_settings.SECTION_POPULIMIT as 'Population',\n" +
"allsections_settings.ROOM_ASSGN as 'Room', allsections_settings.YRLEVEL_ASSGN as 'Year Level', allsections_settings.SCHOOL_YEAR as 'School Year', allsections_settings.SESSION_ASSIGNED as 'Session'\n" +
"FROM allsections_list\n" +
"RIGHT JOIN allsections_settings\n" +
"ON allsections_list.SECTION_ID = allsections_settings.SECTION_ID\n" +
"WHERE SECTION_NAME LIKE ? ESCAPE '!'\n" +
"ORDER BY allsections_list.SECTION_ID";

if (searchSection.isEmpty())
{
JOptionPane.showMessageDialog(null, "Please fill up this fields");
}
else
try (Connection myConn = DBUtil.connect();
PreparedStatement myFirstPs = myConn.prepareStatement(searchSECTIONSETTINGS);)
{
myFirstPs.setString(1, "%" +searchSection +"%");
try (ResultSet myFirstRs = myFirstPs.executeQuery())
{
int resultCounter = 0;
while (myFirstRs.next())
{
String name = myFirstRs.getString(2);
sectionJTable.setModel(DbUtils.resultSetToTableModel(myFirstRs));
resultCounter++;
}//end of loop myFirstRs (ResultSet)

if (resultCounter > 0)//If exist
{
JOptionPane.showMessageDialog(null, "Data Found");
}
else//If not exist
JOptionPane.showMessageDialog(null, "No Data Found");
}//end of try myFirstRs (ResultSet)

}//end of try myFirstPs (PreparedStatement)

catch (SQLException e)
{
DBUtil.processException(e);
}//end of catch
}

关于java - 为什么 select 语句总是返回最后插入的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36290835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com