jdbc oracle 11g PreparedStatement未产生结果

huangapple go评论78阅读模式
英文:

jdbc oracle 11g PreparedStatement not producing results

问题

以下是您要翻译的代码部分:

我试图根据委员会列上的条件使用jdbc检索数据使用Statement可以产生期望的结果但使用PreparedStatement则不行我无法弄清楚出了什么问题请帮助以下是使用Statement和使用PreparedStatement的两个程序以及我的表结构

import java.sql.*;

class SelectPrepared {
    public static void main(String args[]) {

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");

            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "abcd", "abcd");
            String sql = "select * from tatuserinfo where committee = 'GENERAL'";
            Statement stmt = con.createStatement();
            // stmt.setString(1, "GENERAL");//1 specifies the first parameter in the query

            ResultSet myRs = stmt.executeQuery(sql);
            while (myRs.next()) {
                System.out.println(myRs.getString(1) + myRs.getString(2) + myRs.getString(3) + myRs.getString(4)
                        + myRs.getString(5) + myRs.getString(6) + myRs.getString(7) + myRs.getString(8));
            }
            con.close();

        } catch (Exception e) {
            System.out.println(e);
        }

    }
}

import java.sql.*;

class SelectPreparedOne {
  public static void main(String args[]) {

    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
      String s = "GENERAL";
      Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "abcd", "abcd");
      String sql = "select * from tatuserinfo where committee = ?";
      PreparedStatement stmt = con.prepareStatement(sql);
      stmt.setString(1, s);// 1 specifies the first parameter in the query

      ResultSet myRs = stmt.executeQuery();
      while (myRs.next()) {
        System.out.println(myRs.getString(1) + myRs.getString(2) + myRs.getString(3) + myRs.getString(4)
            + myRs.getString(5) + myRs.getString(6) + myRs.getString(7) + myRs.getString(8));
      }
      con.close();

    } catch (Exception e) {
      System.out.println(e);
    }

  }
}

**表结构**
<pre>
USERNAME          VARCHAR2(40) 
PASSWORD          VARCHAR2(40) 
ROLE              VARCHAR2(40) 
NAME              VARCHAR2(40) 
DESIGNATION       VARCHAR2(40) 
DEPARTMENT        VARCHAR2(40) 
EMAILID           VARCHAR2(40) 
COMMITTEE         CHAR(15)     
</pre>
英文:

I am trying fetch data based on a condition on committee column using jdbc.Using Statement it produces the desired result but using PreparedStatement it does not.I cannot figure out what has gone wrong.Kindly help.Here is both the programs one using Statement and the other one using PreparedStatement and my table structure as well

import java.sql.*;
class SelectPrepared {
public static void main(String args[]) {
try {
Class.forName(&quot;oracle.jdbc.driver.OracleDriver&quot;);
Connection con = DriverManager.getConnection(&quot;jdbc:oracle:thin:@localhost:1521:xe&quot;, &quot;abcd&quot;,&quot;abcd&quot;);
String sql = &quot;select * from tatuserinfo where committee = &#39;GENERAL&#39;&quot;;
Statement stmt = con.createStatement();
// stmt.setString(1,&quot;GENERAL&quot;);//1 specifies the first parameter in the query
ResultSet myRs = stmt.executeQuery(sql);
while (myRs.next()) {
System.out.println(myRs.getString(1) + myRs.getString(2) + myRs.getString(3) + myRs.getString(4)
+ myRs.getString(5) + myRs.getString(6) + myRs.getString(7) + myRs.getString(8));
}
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
import java.sql.*;
class SelectPreparedOne {
public static void main(String args[]) {
try {
Class.forName(&quot;oracle.jdbc.driver.OracleDriver&quot;);
String s = &quot;GENERAL&quot;;
Connection con = DriverManager.getConnection(&quot;jdbc:oracle:thin:@localhost:1521:xe&quot;, &quot;abcd&quot;,&quot;abcd&quot;);
String sql = &quot;select * from tatuserinfo where committee = ?&quot;;
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, s);// 1 specifies the first parameter in the query
ResultSet myRs = stmt.executeQuery();
while (myRs.next()) {
System.out.println(myRs.getString(1) + myRs.getString(2) + myRs.getString(3) + myRs.getString(4)
+ myRs.getString(5) + myRs.getString(6) + myRs.getString(7) + myRs.getString(8));
}
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}

Table structure
<pre>
USERNAME VARCHAR2(40)
PASSWORD VARCHAR2(40)
ROLE VARCHAR2(40)
NAME VARCHAR2(40)
DESIGNATION VARCHAR2(40)
DEPARTMENT VARCHAR2(40)
EMAILID VARCHAR2(40)
COMMITTEE CHAR(15)
</pre>

答案1

得分: 1

TL/DR: 不要使用 char,使用 varchar2

CHAR(15) 会在后面添加空格以使列达到15个字符的长度,因此该列包含值 &#39;GENERAL &#39;,而这不等于提供的值 &#39;GENERAL&#39;

正确的修复方法是将列更改为 VARCHAR2(15)

一个中间的不太美观的解决方法(直到你修复列定义)是使用 trim 函数:

where trim(committee) = ?;
英文:

TL/DR: don't use char use varchar2

CHAR(15) gets blank padded to 15 characters, so the column contains the value &#39;GENERAL &#39; and that's not equal to the supplied value of &#39;GENERAL&#39;

The correct fix is to change the column to VARCHAR2(15)

An intermediate ugly workaround (until you fix the column definition) is to use trim:

where trim(committee) = ?;

huangapple
  • 本文由 发表于 2020年8月4日 19:39:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63246116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定