如何忽略空值?
How do I ignore the null values?
在我的数据库中,我有 4 个数据字段,我还有一个方法 (getWalletName
),我发送一个 SQL 查询来只检索 4 个数据中的一个,但是之后检索并显示结果。我可以像这样看到所需数据旁边的其他数据:
SELECT Name FROM wallets WHERE Location ='ROOME' ;
[unknown,unknown,unknown,Omar]
我希望它像那样显示的地方 [Omar]
,请记住不要修改我的构造函数,因为我需要它们以这种方式显示。我试过下面 wallet
class 中的代码,但没有用!
任何帮助将不胜感激
wallet class :
public class wallet {
private String Name;
private String Location;
private String Tag;
private String Reader;
public wallet(String Name , String Location, String Tag, String Reader)
{
this.Name = Name;
this.Location = Location;
this.Tag = Tag;
this.Reader = Reader;
}
public wallet(String Reader)
{
this.Reader = Reader;
this.Name = "unknown";
this.Location = "unknown";
this.Tag = "unknown";
}
public wallet(String Location,String Tag) {
this.Location = Location;
this.Tag = Tag;
}
public String getWalletName()
{
return Name;
}
public void setWalletName(String Name)
{
this.Name = Name;
}
public String getWalletLocation()
{
return Location;
}
public void setWalletLocation(String Location)
{
this.Location = Location;
}
public String getWalletTag()
{
return Tag;
}
public void setWalletTag(String Tag)
{
this.Tag = Tag;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
if(Objects.nonNull(this.Name)) {
sb.append(this.Name+",");
}
if(Objects.nonNull(this.Location)) {
sb.append(this.Location+",");
}
if(Objects.nonNull(this.Tag)) {
sb.append(this.Tag+",");
}
if(Objects.nonNull(this.Reader)) {
sb.append(this.Reader);
}
return sb.toString();
}
}
getWalletName method:
public ArrayList<wallet> getWalletName(String Location) throws SQLException {
System.out.println("----------------");
System.out.println("Retrieve the wallet name");
//Connection dbConnection = null;
//Statement statement = null;
ResultSet resultset = null;
String query = "SELECT Name FROM wallets WHERE Location ='" + Location + "' ;";
ArrayList<wallet> WalletsList = new ArrayList<>();
try {
getConnection();
//statement = dbConnection.createStatement();
System.out.println(query);
resultset = stmt.executeQuery(query);
while (resultset.next()) {
String Name = resultset.getString("Name");
WalletsList.add(new wallet (Name));
}
}
finally {
// closeConnection();
if (resultset !=null){
resultset.close();
}
if (stmt !=null) {
stmt.close();
}
if (resultset !=null) {
closeConnection();
}
}
return WalletsList;
}
把这个放在下面String Name = resultset.getString("Name");
,应该可以
If resultset.getString("Name") != null {
WalletsList.add(new wallet (Name));
}
如果你不想改变你的构造函数,只有一个办法:
class wallet {
private String Name;
private String Location;
private String Tag;
private String Reader;
public wallet(String Name, String Location, String Tag, String Reader) {
this.Name = Name;
this.Location = Location;
this.Tag = Tag;
this.Reader = Reader;
}
public wallet(String Reader) {
this.Reader = Reader;
this.Name = "unknown";
this.Location = "unknown";
this.Tag = "unknown";
}
public wallet(String Location, String Tag) {
this.Location = Location;
this.Tag = Tag;
}
public String getWalletName() {
return Name;
}
public void setWalletName(String Name) {
this.Name = Name;
}
public String getWalletLocation() {
return Location;
}
public void setWalletLocation(String Location) {
this.Location = Location;
}
public String getWalletTag() {
return Tag;
}
public void setWalletTag(String Tag) {
this.Tag = Tag;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if (isPrintableValue(this.Name)) {
sb.append(this.Name + ",");
}
if (isPrintableValue(this.Location)) {
sb.append(this.Location + ",");
}
if (isPrintableValue(this.Tag)) {
sb.append(this.Tag + ",");
}
if (isPrintableValue(this.Reader)) {
sb.append(this.Reader);
}
return sb.toString();
}
protected boolean isPrintableValue(String value) {
return (value != null && !value.equals("unknown"));
}
}
public class Test {
public static void main(String[] args) {
wallet wallet = new wallet("Omar");
System.out.println(wallet);
}
}
输出:
Omar
我建议 toString()
钱包 class 的替代版本,也许更紧凑:
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
sb.append(checkValue(this.Name,","));
sb.append(checkValue(this.Location,","));
sb.append(checkValue(this.Tag,","));
sb.append(checkValue(this.Reader,""));
sb.append("]");
return sb.toString();
}
protected String checkValue(String value, String separator) {
return (value != null && !value.equals("unknown")) ? (value+separator) : "";
}
此外,首先我建议使用CamelCase来编写方法、变量、classes、包和常量的名称:这是一种标准的表示法(你不必强制,它的工作原理是一样的,但是对于开发人员社区来说更容易理解)。所以最好每个单词或缩写都以大写字母开头,或者第一个单词以小写字母开头,其余全部大写。
其次,我建议用户使用 PreparedStatement 而不是简单的 Statement,因为出于安全原因更可取:动态构建 Statement 容易受到 SQL 注入的攻击(使用 PreparedStatement 可以避免在谓词中引用值时出现的语法错误)。
所以代替:
String query = "SELECT Name FROM wallets WHERE Location ='" + Location + "' ;";
您可以尝试使用:
String query = "SELECT Name FROM wallets WHERE Location = ? ";
连同类似的东西(假设 conn
是你的数据库连接):
try {
String query = "SELECT Name FROM wallets WHERE Location = ? ";
try (PreparedStatement stmt = conn.prepareStatement(query); ResultSet resultset = stmt.executeQuery(query)) {
while (resultset.next()) {
String Name = resultset.getString("Name");
WalletsList.add(new wallet(Name));
}
}
} catch (SQLException sQLException) {
sQLException.printStackTrace();
}
请注意,我使用 "try-with-resource" 语法来避免关闭结果集和语句的问题(即带有 resultset.close()、stament.close() 等的 finally 子句这是冗长的,如果面对不正确,有时可能会导致一些编码错误。
最好的是
在我的数据库中,我有 4 个数据字段,我还有一个方法 (getWalletName
),我发送一个 SQL 查询来只检索 4 个数据中的一个,但是之后检索并显示结果。我可以像这样看到所需数据旁边的其他数据:
SELECT Name FROM wallets WHERE Location ='ROOME' ;
[unknown,unknown,unknown,Omar]
我希望它像那样显示的地方 [Omar]
,请记住不要修改我的构造函数,因为我需要它们以这种方式显示。我试过下面 wallet
class 中的代码,但没有用!
任何帮助将不胜感激
wallet class :
public class wallet {
private String Name;
private String Location;
private String Tag;
private String Reader;
public wallet(String Name , String Location, String Tag, String Reader)
{
this.Name = Name;
this.Location = Location;
this.Tag = Tag;
this.Reader = Reader;
}
public wallet(String Reader)
{
this.Reader = Reader;
this.Name = "unknown";
this.Location = "unknown";
this.Tag = "unknown";
}
public wallet(String Location,String Tag) {
this.Location = Location;
this.Tag = Tag;
}
public String getWalletName()
{
return Name;
}
public void setWalletName(String Name)
{
this.Name = Name;
}
public String getWalletLocation()
{
return Location;
}
public void setWalletLocation(String Location)
{
this.Location = Location;
}
public String getWalletTag()
{
return Tag;
}
public void setWalletTag(String Tag)
{
this.Tag = Tag;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
if(Objects.nonNull(this.Name)) {
sb.append(this.Name+",");
}
if(Objects.nonNull(this.Location)) {
sb.append(this.Location+",");
}
if(Objects.nonNull(this.Tag)) {
sb.append(this.Tag+",");
}
if(Objects.nonNull(this.Reader)) {
sb.append(this.Reader);
}
return sb.toString();
}
}
getWalletName method:
public ArrayList<wallet> getWalletName(String Location) throws SQLException {
System.out.println("----------------");
System.out.println("Retrieve the wallet name");
//Connection dbConnection = null;
//Statement statement = null;
ResultSet resultset = null;
String query = "SELECT Name FROM wallets WHERE Location ='" + Location + "' ;";
ArrayList<wallet> WalletsList = new ArrayList<>();
try {
getConnection();
//statement = dbConnection.createStatement();
System.out.println(query);
resultset = stmt.executeQuery(query);
while (resultset.next()) {
String Name = resultset.getString("Name");
WalletsList.add(new wallet (Name));
}
}
finally {
// closeConnection();
if (resultset !=null){
resultset.close();
}
if (stmt !=null) {
stmt.close();
}
if (resultset !=null) {
closeConnection();
}
}
return WalletsList;
}
把这个放在下面String Name = resultset.getString("Name");
,应该可以
If resultset.getString("Name") != null {
WalletsList.add(new wallet (Name));
}
如果你不想改变你的构造函数,只有一个办法:
class wallet {
private String Name;
private String Location;
private String Tag;
private String Reader;
public wallet(String Name, String Location, String Tag, String Reader) {
this.Name = Name;
this.Location = Location;
this.Tag = Tag;
this.Reader = Reader;
}
public wallet(String Reader) {
this.Reader = Reader;
this.Name = "unknown";
this.Location = "unknown";
this.Tag = "unknown";
}
public wallet(String Location, String Tag) {
this.Location = Location;
this.Tag = Tag;
}
public String getWalletName() {
return Name;
}
public void setWalletName(String Name) {
this.Name = Name;
}
public String getWalletLocation() {
return Location;
}
public void setWalletLocation(String Location) {
this.Location = Location;
}
public String getWalletTag() {
return Tag;
}
public void setWalletTag(String Tag) {
this.Tag = Tag;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if (isPrintableValue(this.Name)) {
sb.append(this.Name + ",");
}
if (isPrintableValue(this.Location)) {
sb.append(this.Location + ",");
}
if (isPrintableValue(this.Tag)) {
sb.append(this.Tag + ",");
}
if (isPrintableValue(this.Reader)) {
sb.append(this.Reader);
}
return sb.toString();
}
protected boolean isPrintableValue(String value) {
return (value != null && !value.equals("unknown"));
}
}
public class Test {
public static void main(String[] args) {
wallet wallet = new wallet("Omar");
System.out.println(wallet);
}
}
输出:
Omar
我建议 toString()
钱包 class 的替代版本,也许更紧凑:
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
sb.append(checkValue(this.Name,","));
sb.append(checkValue(this.Location,","));
sb.append(checkValue(this.Tag,","));
sb.append(checkValue(this.Reader,""));
sb.append("]");
return sb.toString();
}
protected String checkValue(String value, String separator) {
return (value != null && !value.equals("unknown")) ? (value+separator) : "";
}
此外,首先我建议使用CamelCase来编写方法、变量、classes、包和常量的名称:这是一种标准的表示法(你不必强制,它的工作原理是一样的,但是对于开发人员社区来说更容易理解)。所以最好每个单词或缩写都以大写字母开头,或者第一个单词以小写字母开头,其余全部大写。 其次,我建议用户使用 PreparedStatement 而不是简单的 Statement,因为出于安全原因更可取:动态构建 Statement 容易受到 SQL 注入的攻击(使用 PreparedStatement 可以避免在谓词中引用值时出现的语法错误)。
所以代替:
String query = "SELECT Name FROM wallets WHERE Location ='" + Location + "' ;";
您可以尝试使用:
String query = "SELECT Name FROM wallets WHERE Location = ? ";
连同类似的东西(假设 conn
是你的数据库连接):
try {
String query = "SELECT Name FROM wallets WHERE Location = ? ";
try (PreparedStatement stmt = conn.prepareStatement(query); ResultSet resultset = stmt.executeQuery(query)) {
while (resultset.next()) {
String Name = resultset.getString("Name");
WalletsList.add(new wallet(Name));
}
}
} catch (SQLException sQLException) {
sQLException.printStackTrace();
}
请注意,我使用 "try-with-resource" 语法来避免关闭结果集和语句的问题(即带有 resultset.close()、stament.close() 等的 finally 子句这是冗长的,如果面对不正确,有时可能会导致一些编码错误。
最好的是