【问题标题】:When I have click the button all the records in the table is displayed in the table当我单击按钮时,表中的所有记录都显示在表中
【发布时间】:2016-07-27 12:09:05
【问题描述】:

当我点击按钮时,表中的所有记录都会显示在表中。我在我的 sql 查询中使用了 like 和 equal 运算符,它确实在第二次第一次将所有数据检索到表中时起作用?谁能告诉我代码有什么问题?

 try{
            String url="jdbc:sqlserver://localhost:1433;databaseName=gym2 ";
                String username = "mali";
                String password = "12345";
                Connection con =DriverManager.getConnection(url,username,password);
                Statement st = con.createStatement ();
                ResultSet rs;
                String empid =jLabel3.getText()+jTextField1.getText();
                String name = jTextField2.getText();
               // String d =jComboBox1.getSelectedItem().toString();
       String sql = "SELECT emp_id,firstname,designation,full_address,gender,land,monthly_sal FROM employee_reg where (emp_id = '"+empid+"' or firstname LIKE '"+name+"%')";


        rs= st.executeQuery(sql);
       jTable1.setModel(DbUtils.resultSetToTableModel(rs));
            }
            catch(Exception e){

            } 
        }                 

【问题讨论】:

  • 永远不要这样做:catch(Exception e){ }。至少打印堆栈跟踪,你会看到发生了什么
  • 了解准备好的语句
  • 你的问题不清楚
  • 请编辑您的问题,并明确您想要什么。也许您可以向我们展示表结构和一些示例条目。
  • 打印您正在创建的查询。 System.out.println(sql);第一次查询可能有机会,firstName 为空。例如您的查询可能看起来像 SELECT emp_id,firstname,designation,full_address,gender,land,monthly_sal FROM employee_reg where (emp_id = '2' or firstname LIKE '%'); LIKE '%' 将获取所有记录。

标签: java sql database sql-like


【解决方案1】:

我认为原始问题中的“第一次”和“第二次”实际上代表“jTextField1 value defined”和“jTextField2 value defined”。

生成的带有空 jTextField2(名称)的 SQL 查询字符串将导致:

SELECT emp_id,firstname,designation,full_address,gender,land,monthly_sal
FROM employee_reg where (emp_id = '...' or firstname LIKE '%');

firstname LIKE '%' 将匹配 firstname 不为 NULL 的所有记录,并且由于条件由OR 分隔,emp_id = '...' 条件的影响很小。

要真正“在存在时使用过滤器”场景,如果相应字段为空白,我们需要排除条件:

String     url       = "jdbc:sqlserver://localhost:1433;databaseName=gym2 ";
String     username  = "mali";
String     password  = "12345";
Connection con       = DriverManager.getConnection(url,username,password);
Statement  st        = con.createStatement ();
ResultSet  rs;

// Prepare the whole emp_id condition string
String     empidCond = " or emp_id = '" + jLabel3.getText()+jTextField1.getText() + "'";
// Clear it if jTextField1 is empty
if(jTextField1.getText().isEmpty()) {empidCond = "";}

// Prepare the whole firstname condition string
String     nameCond  = " or firstname LIKE '" + jTextField2.getText() + "%'";
// Clear it if jTextField2is empty
if(jTextField2.getText().isEmpty()) {nameCond = "";}
// String d          = jComboBox1.getSelectedItem().toString();

// Create SQL statement with one "stub" condition always false
// to "chain" the other conditions to
String sql = "SELECT emp_id,firstname,designation,full_address,gender,land,monthly_sal "+
             "FROM employee_reg "+
             "WHERE 1=2" + empidCond + nameCond;

rs = st.executeQuery(sql);
jTable1.setModel(DbUtils.resultSetToTableModel(rs));

我必须在这里指出,整个内容仅用于教育目的,上面的任何代码都不应该在生产中使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 2021-06-01
    相关资源
    最近更新 更多