Quantcast
Viewing all articles
Browse latest Browse all 3160

The same input list has two different queries in sql server using java db method

I want to establish single connection with sql server for two different queries having same input. Is that acheivable? Here is my code which will establish the connection with database twice for executing queries seperately with same input.

String query1="select ID_Student,Lib_Sec_Book from Library_db1 where ID_Student"; String query2="select ID_Student,Lib_Sec_Book from Library_db2 where ID_Student"; Map<String, String> result1=dba.dbcon(inputListMismatch,query1); Map<String, String> result2=dba.dbcon(inputListMismatch,query2);

these queries have same student IDs as input,I need to find out which ID having book from two library databases.It may mutually exclusive.

saving resultset of query1 and query2 in result1 and result2 respectively two results need to be in single Map. In dba method,

Map<String, String> result=new HashMap<String,String>(); try { String databaseDriver = "net.sourceforge.jtds.jdbc.Driver"; Class.forName(databaseDriver); } catch (Exception e) { e.printStackTrace(); } try { String url = "jdbc:jtds:sqlserver://server;instance="; java.sql.Connection con =DriverManager.getConnection(url); System.out.println("Connection created"); String sqlQuery=query+" "+getIn(list.size()); PreparedStatement preparedStatement = con.prepareStatement(sqlQuery); for(int i=0; i<list.size(); i++) preparedStatement.setString(i+1, (String) list.get(i)); ResultSet rs = preparedStatement.executeQuery(); ResultSetMetaData rsMeta=rs.getMetaData(); int colcount=rsMeta.getColumnCount(); result.put(rsMeta.getColumnName(1),rsMeta.getColumnName(2)); while(rs.next()) { result.put(rs.getString(1),rs.getString(2)); } rs.close(); preparedStatement.close(); con.close(); } catch (Exception e1) { e1.printStackTrace(); } return result; }

getting input list

static String getIn(int numParams) { StringBuilder builder = new StringBuilder("IN (?"); for(int i=1; i<numParams; i++) builder.append(",?"); builder.append(")"); return builder.toString(); }

Now I am getting two different resultset, I want to pass two queries in a single instance. I tried union,It is throwing "Parameter has not been set" error. and I have also tried 'allowMultiQueries=TRUE" that is not helping. Can you suggest better way of doing this?

I am not sure what you want,but I try to give you some suggestions.

If you are looking for a solution to improve performance,use two threads to query simultaneously. Use http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html to control threads.

If you want to merge two queries into one query , try the following code to build sql.

String sqlQuery=query+" "+getIn(list1.size()) +" union "+ getIn(list2.size()); PreparedStatement preparedStatement = con.prepareStatement(sqlQuery); for(int i=0; i<list1.size(); i++) preparedStatement.setString(i+1, (String) list.get(i)); for(int i=0; i<list2.size(); i++) preparedStatement.setString(list1.size()+i+1, (String) list.get(i));


Viewing all articles
Browse latest Browse all 3160

Trending Articles