如何获取主键以执行select语句 [英] How to get the primary key to execute an select statement

查看:207
本文介绍了如何获取主键以执行select语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to update my table using Java JDBC. But I don't have any idea how can I call my primary key using prepared statement. I try to make an USER_ID object for the column of my database but don't know how where to start. How will I determine if I already updated my Database?

插入

   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);
       }
    }
   }

如果我插入一个新值,它将把新值加载到数据库中.

Here if I insert a new value it will load the new values to database.

选择

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);
    }
  }

SCREENSHOT

填写 searchName和searchDepartment文本字段的字段后.如您所见,我在这里点击了搜索"按钮. 我希望将 searchName和searchDepartment 的值打印到 employeeTf和departmentTf 的文本字段中 在这里我将如何使用setText?

After filling out the fields for searchName and searchDepartment textfields. As you can see here I here hit the "Search" button. I want the value of searchName and searchDepartment will print out to the textfield of employeeTf and departmentTf How will I use the setText here?

推荐答案

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

我以为nameTextdeptText是平移文本组件的对象

i supposed nameText and deptText are the object of swing text component

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

if these are not accessible here..then you can create object of all field and put data from result set and return the object and use there.

这篇关于如何获取主键以执行select语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆