【问题标题】:How to get the primary key to execute an select statement [duplicate]如何获取主键以执行选择语句[重复]
【发布时间】:2016-06-16 09:05:38
【问题描述】:

我正在尝试使用 Java JDBC 更新我的表。但我不知道如何使用准备好的语句调用我的主键。我尝试为我的数据库列创建一个USER_ID 对象,但不知道如何开始。如何确定我是否已经更新了我的数据库?

插入

   private void addBtnActionPerformed(java.awt.event.ActionEvent evt) {
   String inputEmployee = employeeTf.getText();
   String inputDepartment = departmentTf.getText();

   if(inputEmployee.isEmpty() && inputDepartment.isEmpty()){
         JOptionPane.showMessageDialog(null, "Please fill up!");
    }
    else if(inputEmployee.isEmpty()){
        JOptionPane.showMessageDialog(null, "Please enter your username");
    }
    else if(inputDepartment.isEmpty()){
        JOptionPane.showMessageDialog(null, "Please enter your password");
    }
    else{
        String myQuery = "INSERT INTO SAMPLE (EMPLOYEENAME,DEPARTMENT) VALUES (?,?)";

        try(Connection con = DBUtilities.getConnection(DBType.JDBC);
        PreparedStatement myPs = con.prepareStatement(myQuery);
            ){

            myPs.setString(1, employeeTf.getText());
            myPs.setString(2, departmentTf.getText());

            myPs.executeUpdate();

            System.out.print("Record is inserted");


        } catch (SQLException ex) {
            DBUtilities.processException(ex);
       }
    }
   }

如果我在这里插入一个新值,它会将新值加载到数据库中。

选择

private void searchBtnActionPerformed(java.awt.event.ActionEvent evt) {
String searchEmployee = searchName.getText(); //SWING COMPONENTS
String searchDept = searchDepartment.getText();//SWING COMPONENTS

    String selectQuery = "SELECT EMPLOYEENAME,DEPARTMENT FROM SAMPLE WHERE USER_ID = ?";

    try {
        Connection con = DBUtilities.getConnection(DBType.JDBC);
        PreparedStatement myPs = con.prepareStatement(selectQuery,ResultSet.CONCUR_UPDATABLE,ResultSet.TYPE_SCROLL_INSENSITIVE);

        ResultSet myRs = myPs.executeQuery();

        while(myRs.next()){
           String name = myRs.getString(1);
           String department = myRs.getString(2);
        }

    } catch (SQLException ex) {
        DBUtilities.processException(ex);
    }
  }

屏幕截图

填写searchName 和searchDepartment 文本字段 的字段后。正如你在这里看到的,我在这里点击了“搜索”按钮。 我希望 searchName 和 searchDepartment 的值将打印到 employeeTf 和 departmentTf 的文本字段中 我将如何在这里使用 setText?

【问题讨论】:

  • 发布您的挥杆组件定义
  • 已经编辑了swing组件。谢谢。请再次查看我的帖子。

标签: java mysql swing jdbc


【解决方案1】:
 while(myRs.next()){
    String name = myRs.getString(1);
    String department = myRs.getString(2);
    nameText.setText(name);
    deptText.setText(department);
 }

我认为nameTextdeptText是swing文本组件的对象

如果这些在此处无法访问..那么您可以创建所有字段的对象并从结果集中放入数据并返回对象并在那里使用。

【讨论】:

  • 我为 nameText 和 deptText 创建了一个对象。当我尝试使用 setText() 时找不到符号?我错过了什么吗?
  • 贴出swing组件的代码
  • 已编辑。我只是使用拖放到这个。用于示例目的。我知道我已经接近答案了。
  • 你试过这个:nameText.setText(name);
  • 是的,但是当我尝试为 nameText 创建一个对象时,setText 会给我一个错误,找不到符号。方法 setText(String)
猜你喜欢
  • 1970-01-01
  • 2014-02-20
  • 2015-11-24
  • 1970-01-01
  • 2019-12-30
  • 2016-06-12
  • 2020-03-11
  • 2020-02-23
  • 1970-01-01
相关资源
最近更新 更多