简体   繁体   English

JDBC从SQL查询中解析int

[英]JDBC parsing int from SQL query

I am pretty new to programming in Java and am having a little difficulty. 我对使用Java编程非常陌生,并且遇到了一些困难。 I have searched around but could not get an exact answer for what I am trying to do. 我已经四处搜寻,但无法获得确切答案。

Basically, I have created a 3-tier application that uses servlets running on a server to access a mySQL database using JDBC. 基本上,我创建了一个3层应用程序,该应用程序使用服务器上运行的servlet来使用JDBC访问mySQL数据库。

For the most part, my application runs fine. 在大多数情况下,我的应用程序运行良好。 I am able to run my queries and display my results. 我能够运行查询并显示结果。

The problem is, I need to implement some logic based on my query, which is a String. 问题是,我需要根据查询(字符串)实现一些逻辑。

For example, 例如,

ex1_query: insert into parts values ('S5', 'P6', 'J7', 50); ex1_query: insert into parts values ('S5', 'P6', 'J7', 50);

ex2_query: insert into parts values ('S5', 'P6', 'J7', 400); ex2_query: insert into parts values ('S5', 'P6', 'J7', 400);

My queries are being submitted through an HTML form as single string. 我的查询是通过HTML表单作为单个字符串提交的。 As you can see, the only difference between ex1_query and ex2_query is their last value (50 & 400). 如您所见,ex1_query和ex2_query之间的唯一区别是它们的最后一个值(50和400)。

The logic is, that if that last value is >=100, I need to implement some stuff… 逻辑是,如果最后一个值> = 100,则需要实现一些东西…

If that value is <100, just run the query as specified. 如果该值小于100,则只需按照指定的条件运行查询。

My queries will always be entered in that format, but that last value may vary from 0 - 1000. 我的查询将始终以该格式输入,但最后一个值可能在0-1000之间变化。

So my question is… how can I parse just that last int from the string? 所以我的问题是……我怎样才能仅解析字符串中的最后一个int? So I can determine whether I should or should not implement some specific logic? 因此,我可以确定是否应该实施某些特定的逻辑?

I know it sounds pretty simple, but I am having a little trouble. 我知道这听起来很简单,但是我遇到了一些麻烦。 Any help or guidance would be appreciated. 任何帮助或指导,将不胜感激。 Thanks 谢谢

User Prepared Statement 用户准备的声明

 String param1= "S5";
  String param2= "p6";
  String param3= "j7"; 
  int param4= 50 0r 400  ; 

 public void someMethod(String param1,String param2,String param3,String param4){

if(param4 >= 100){
//your stuff
param4 =  ; //your new value;
}

query =  insert into parts values (?, ?, ?,?);

And Execute your Query 并执行查询

If you do not have any control about the query you might want to befriend java.util.regex and parse for something like: 如果您对查询没有任何控制权,则可能希望与java.util.regex成为朋友并进行如下分析:

"^\s*insert\s+into\s+([A-Za-z0-9]+)\s+values\s+\(\s*('\w+'\s*,\s*){3}([0-9]+)\)\s*$"

This is actually an old-style grep regex but a regex with the same function is possible for java.util.regex. 这实际上是老式的grep regex,但是java.util.regex可以使用具有相同功能的regex。 It checks whether the string follows your pattern. 它检查字符串是否遵循您的模式。 submatch #1 should give you your table name. 子匹配项#1应该为您提供表格名称。 submatch #2-#4 should give you the values 1-3 and submatch #5 contains the number (as string of course) you're looking for. submatch#2-#4应该为您提供值1-3,submatch#5包含您要查找的数字(当然是字符串)。 Also a nice way to reject queries you do not like. 拒绝您不喜欢的查询的一种好方法。

But If you don't wanted to go with Preparedstatement 但是,如果您不想使用Preparedstatement

then here is one more option 那么这是另一个选择

    String ex1_query = "insert into parts values ('S5', 'P6', 'J7', 50);";

    String lastValue = ex1_query.substring( ex1_query.lastIndexOf(",")+1, ex1_query.lastIndexOf(")")).trim();

 Integer lastNumber = null;
        try {
            lastNumber = Integer.parseInt(lastValue);
        } catch (NumberFormatException e) {
            // if last value is not int then handle here 
            e.printStackTrace();
        } 
        if(lastNumber >=100){

        }

In lastValue you can get 50/400 or any value and do your stuff 在lastValue中,您可以获得50/400或任何值并执行您的任务

If your query will remain same except that integer value, you can use substring . 如果您的查询除该integer数值外将保持不变,则可以使用substring

String query = "insert into parts values ('S5', 'P6', 'J7', 50);"
int value = Integer.parseInt(query.substring(43, query.length()-2));

Or you can use regular expression to accomplish this. 或者,您可以使用正则表达式来完成此操作。

String query = "insert into parts values ('S5', 'P6', 'J7', 50);";
Pattern pattern = Pattern.compile("(?<=(.+,\\s?))(\\d+)(?=(\\);))");
int value = Integer.parseInt(pattern.matcher(query).group());

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

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