mysql 如何用数据库条目数据预加载GUI?

olhwl3o2  于 2023-04-05  发布在  Mysql
关注(0)|答案(1)|浏览(118)

我试图用数据库条目的数据预加载GUI。基本上在以前的JForm中我已经要求用户输入一个名称,这个类接受这个名称并用数据库条目填充现有的文本字段和区域,虽然它似乎不工作,因为它不预加载任何现有的字段,虽然它确实存在于数据库中。如果任何人可以帮助它将不胜感激,因为这是由于很快!

public void presetPage(){
    modifyRecipe m = new modifyRecipe();
    String name = m.getSR();
    try{
        String url = "jdbc:derby://localhost:1527/Cookbook";
        Connection conn = DriverManager.getConnection(url);
        String sql = "SELECT * from RECIPES where name=?";
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setString(1, name);

        ResultSet rs = pst.executeQuery();
        boolean found = false;

        while(rs.next()){
            if(rs.getString(1).equalsIgnoreCase(name)){
                JOptionPane.showMessageDialog(null, "Found " + name );
                found = true;

                txtName.setText(rs.getString(1));
                txtSubcatagory.setText(rs.getString(2));
                cmbCatagory.getSelectedItem().toString();
                txtIngredients.setText(rs.getString(4));
                txtInstructions.setText(rs.getString(5));
                txtMod.setText(rs.getString(6));


            }else if(!found){
            JOptionPane.showMessageDialog(null, "Item Not Found!");
            this.dispose();

        }
        }
ccrfmcuu

ccrfmcuu1#

连接到database
使用JDBC驱动程序连接到数据库,如下所示:

String url = "jdbc:derby://localhost:1527/Cookbook";
Connection conn = DriverManager.getConnection(url);

准备SELECT查询:
编写一个SELECT查询,从database获取数据。使用PreparedStatement将参数绑定到查询:

String sql = "SELECT * from RECIPES where name=?";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1, name);

执行查询:
执行查询并获取ResultSet,同时检查它是否有任何数据:

ResultSet rs = pst.executeQuery();
if (rs.next()) {
  // the result set has data
} else {
  // the result set is empty
}

GUI中加载数据:
使用来自ResultSet的数据加载GUI

txtName.setText(rs.getString(1));
txtSubcatagory.setText(rs.getString(2));
cmbCatagory.setSelectedItem(rs.getString(3));
txtIngredients.setText(rs.getString(4));
txtInstructions.setText(rs.getString(5));
txtMod.setText(rs.getString(6));

GUI组件必须在setText()setSelectedItem()方法之前初始化。
GUI必须初始化,否则数据将无法加载。

public void presetPage() {
  //modifyRecipe m = new modifyRecipe();
  //String name = m.getSR();
  try {
    String url = "jdbc:derby://localhost:1527/Cookbook";
    Connection conn = DriverManager.getConnection(url);
    String sql = "SELECT * from RECIPES where name=?";
    //PreparedStatement pst = conn.prepareStatement(sql);
    //pst.setString(1, name);
    ResultSet rs = pst.executeQuery();
    if (rs.next()) {
      txtName.setText(rs.getString(1));
      txtSubcatagory.setText(rs.getString(2));
      cmbCatagory.setSelectedItem(rs.getString(3));
      txtIngredients.setText(rs.getString(4));
      txtInstructions.setText(rs.getString(5));
      txtMod.setText(rs.getString(6));
    } else {
      //JOptionPane.showMessageDialog(null, "Item Not Found!");
      //this.dispose();
    }
  } catch (SQLException ex) {
    ex.printStackTrace();
  }
}

我们假设GUI组件已初始化,SQL查询返回一行。如果查询返回多行,则必须适当处理。

相关问题