简体   繁体   English

如何传递空值以在plsql中进行选择

[英]How to pass null value to select in plsql

I am trying to select some values in my table but when Invoicedate is null I got exception please help me! 我试图在表中选择一些值,但是当Invoicedate为null时,出现异常,请帮帮我!

my code : 我的代码:

query="select vend_name,vend_no from account where inv_date = '"+Invoicedate+"' or vend_no = "+Vendorno+" LIMIT 500";

please give any possible answer to me. 请给我任何可能的答案。

That's because in your code it will become inv_date = 'null' not inv_date is null . 那是因为在您的代码中它将变为inv_date = 'null'而不是inv_date is null

You need to do inv_date "+(Invoiceddate==null?"is null":"= '"+Invoiceddate+"'")+" or 您需要执行inv_date "+(Invoiceddate==null?"is null":"= '"+Invoiceddate+"'")+" or

In general this is a bad way to do things though as you are open to SQL injection attacks and all sorts of other similar problems. 通常,这是一种不好的处理方式,因为您容易受到SQL注入攻击和各种其他类似问题的影响。

Use a PreparedStatement and all this will be handled for you: 使用PreparedStatement ,所有这些将为您处理:

PreparedStatement query = connection.prepareStatement("select vend_name,vend_no from account where inv_date = ? or vend_no = ? LIMIT ?");
query.setDate(1, Invoicedate);
... etc

Note also that you should follow the Java style conventions in variable naming etc, it will help people work with your code. 还要注意,在变量命名等方面应该遵循Java样式约定,这将帮助人们使用您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM