简体   繁体   English

使用jdbc对数据库进行多次查询

[英]Multiple queries on database using jdbc

I have a text file from where i read values 我有一个从中读取值的文本文件

FileInputStream file = new FileInputStream("C:/workspace/table_export.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(file));
String line = null;
while( (line = br.readLine())!= null )
{
        String [] tokens = line.split("\\s+");
        String var_1 = tokens[0];
        System.out.println(var_1);
        getstaffinfo(var_1,connection);
}

The values read from text file is passed to getstaffinfo method to query the db 从文本文件读取的值传递到getstaffinfo方法以查询数据库

public static String getstaffinfo(String var_1, Connection connection) throws SQLException, Exception
  // Create a statement
   {
   StringBuffer query = new StringBuffer();
   ResultSet rs = null;
   String record = null;
   Statement stmt = connection.createStatement();
   query.delete(0, query.length());
   query.append("select firstname, middlename, lastname from users where employeeid = '"+var_1+"'");
   rs = stmt.executeQuery(query.toString());
       while(rs.next())
       {
   record = rs.getString(1) + " " +
                  rs.getString(2) + " " +
                  rs.getString(3);
  System.out.println(record);
 }
 return record;
}

I get almost 14000 values read from text file which is passed to getstaffinfo method, all database activities such has loading driver, establishing connectivity all works fine. 我从传递给getstaffinfo方法的文本文件中读取了将近14000个值,所有具有加载驱动程序,建立连接性的数据库活动都工作正常。 But while printing 但是在打印时

it throws error 它引发错误

java.sql.SQLException: ORA-00604: error occurred at recursive SQL level 1
ORA-01000: maximum open cursors exceeded
ORA-01000: maximum open cursors exceeded

Although i understand that this error is to do with database configuration, Is there an efficent way of making one db call and exceute the query for multiple values read from text file. 尽管我知道此错误与数据库配置有关,但是是否存在一种有效的方法来进行一次数据库调用并执行从文本文件读取的多个值的查询。 Any inputs would be of great use. 任何输入将很有用。

Many Thanks in advance!! 提前谢谢了!!

Close ResultSet rs.close(); 关闭ResultSet rs.close(); and Statement stmt.close(); 和语句stmt.close(); after your while loop in getstaffinfo() , preferably inside a finally{} getstaffinfo() while循环之后,最好在finally{}内部

You need to close resultSet via rs.close(); 您需要通过rs.close();关闭resultSet rs.close(); and Statement via stmt.close(); 和声明通过stmt.close();

while(rs.next()){
   record = rs.getString(1) + " " +
            rs.getString(2) + " " +
            rs.getString(3);
   System.out.println(record);
}
rs.close();
stmt.close();

The issue is you are not close ing ResultSet and Statement (though it is good that your are working on one Connection ), try closing resources, the error should not happen. 问题是您没有close ResultSetStatement (尽管您正在使用一个Connection很好),请尝试关闭资源,但不会发生该错误。

Bigger issue is that if you do close, you are hitting DB n number of times where n is number of filtering criteria. 更大的问题是,如果您确实关闭,则您击中数据库的次数为n ,其中n为过滤条件的数量。 One solution to this could be make in clause instead of = selection. 一种解决方案是使用in子句代替= selection。 eg: 例如:

Say total lines = N, divide into x chunks hence make N/x select statements 说总行数= N,分成x个块,因此制作N / x个select语句

For example is N=20000, x=1000; 例如N = 20000,x = 1000; you need to fire 20 selects instead of 20000. 您需要触发20个selects而不是20000个selects

以这种方式关闭结果集和语句

rs.close(); stmt.close();

Best way is to use IN clause in the query and call the method only once. 最好的方法是在查询中使用IN子句,并且仅调用一次该方法。

 FileInputStream file = new FileInputStream("C:/workspace/table_export.txt");
 BufferedReader br = new BufferedReader(new InputStreamReader(file));
 String line = null;
 String var_1 = "";
 while( (line = br.readLine())!= null )
 {
    String [] tokens = line.split("\\s+");
    var_1 = var_1+tokens[0]+",";
    System.out.println(var_1);
}
var_1 = var_1.subString(0,var_1.lastIndexOf(","));
getstaffinfo(var_1,connection);

change getstaffinfo() like this 像这样更改getstaffinfo()

public static List<String> getstaffinfo(String var_1, Connection connection) throws SQLException, Exception
// Create a statement
{
 StringBuffer query = new StringBuffer();
 ResultSet rs = null;
 String record = null;
 List<String> list = new ArrayList<String>();
 Statement stmt = connection.createStatement();
 query.delete(0, query.length());
 query.append("select firstname, middlename, lastname from users where employeeid IN ("+var_1+")");
 try{
  rs = stmt.executeQuery(query.toString());
   while(rs.next())
   {
     record = rs.getString(1) + " " +rs.getString(2) + " " +rs.getString(3);
     System.out.println(record);
     list.add(record);
    }
    }finally{
        stmt.close();
        rs.close();
    }
    return list;
 }

Note : Cant we put more than 1000 values in 'in clause'. 注意:我们不能在'in子句'中放置1000多个值。
Related links. 相关链接。
1. https://forums.oracle.com/thread/235143?start=0&tstart=0 1. https://forums.oracle.com/thread/235143?start=0&tstart=0
2. java.sql.SQLException: - ORA-01000: maximum open cursors exceeded 2. java.sql.SQLException:-ORA-01000:超出最大打开游标

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

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