当我尝试使用Prepared Statement选择一条记录时,它总是给我一个我最近添加的最后插入值。
我首先要做的是在我的第一张表中搜索记录。如果记录存在,则外键表将填充值。我的主键和外键表运行良好。这些值适当地填充到相应的组件,但它没有给我正确的值。有什么帮助吗?
这是引用外键表的主键表,它是第二个表。
选择查询:
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
所以我在这里做的是使用 Right Join 将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顺序给我一个值。
答案 0 :(得分:0)
我刚刚找到了一个最简单的解决方案。我使用Right Join加入了两个表。
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
}