Retrieve multiple ResultSets without using StoredProcedures - java

Is there a way in Java 1.5 to retrieve multiple ResultSet objects from a DB2 database with a single SQL call without resorting to StoredProcedures / CallableStatements?

No. I don't think it is possible to get multiple ResultSet objects in single SQL call. You may refer JDBC API doc.

Related

No value specified for parameter 1

I am using Hiberante to connect to postgres database. I am trying to insert a record into the database. I have the values for the record in a string array which I got from a csv file. This is my dao code
StringBuffer query=new StringBuffer("insert into t_wonlist values(");
for(int i=0;i<67;i++){
query.append(values[i]+",");
}
query.deleteCharAt(query.lastIndexOf(","));
query.append(");");
sessionfactory.getCurrentSession().createSQLQuery(query.toString()).executeUpdate();
System.out.println("Query executed");
sessionfactory.getCurrentSession().flush();
I am using StringBuffer, so that I can append the values into the query using a for loop.
but when I execute the query I am getting the following exception
org.postgresql.util.PSQLException: No value specified for parameter 1.
I am sure that the number of parameters is correct. Can someone help me. Thanks
You're approaching this in a bizarre and backwards manner.
The immediate problem is probably failure to escape/quote a ? in one of the input strings, so PgJDBC thinks it's a query parameter. That doesn't mean you should fix it by escaping/quoting question marks, it's a sign you're taking entirely the wrong approach.
Please read this page on SQL injection and this site.
You're using the Hibernate ORM, so you'd usually be using the JPA interface or the direct Hibernate interface to create new domain objects and persisting them. The typical approach is to new an object, then use the EntityManager.persist method (if using JPA) or the Session.save method (if using Hibernate directly) to persist the entities.
If you want to use direct JDBC instead you should be creating a JDBC PreparedStatement, setting its parameters, and then applying it. See this tutorial. Since you're loading CSV you'd usually do this in a JDBC batch, though this doesn't actually gain you much in PostgreSQL.
Better yet, since you're importing CSV you can probably just use PostgreSQL's built-in COPY command via PgJDBC's CopyManager to stream the changes efficiently into the target table.

running sql update query in java swing

I have a swing desktop application that is
connected to a derby DB.
I use native sql query to select all data from the table
Now I want to update the data in the table with native sql queries
but first I have to retrieve the input data from the swing textfields.
I am able to use the jtextfielld.gettext()
to retrieve that data but how do i set these textfields data to
the update query since the sql query is executed as a string statement.
for example :
String sql = "UPDATE APP.REGISTRY SET LETTER_FROM ='Japan Motors' WHERE id = 7"
so how do execute this query from the swing interface.
Please help.
Have you looked at PreparedStatements ?
Although PreparedStatement objects can be used for SQL statements with
no parameters, you probably use them most often for SQL statements
that take parameters. The advantage of using SQL statements that take
parameters is that you can use the same statement and supply it with
different values each time you execute it.
See the linked doc and note in particular the placeholder capability (using ?s) that allows you to safely insert parameters in your SQL.
It appears you are new to JDBC and looking for a way to execute queries form Swing App. If this is a case, I will refer you to look at JDBC documentation.
In essence, you will need to get a reference to Connection object, then create a PreparedStatement using the connection and then then you can call executeUpdate() method followed by commit() method.
However, Please understand that you have very limited options for getting connection in a swing app. You will need to look at the usage of DriverManager for the way to get connection for a database.

Can you put multiple statements in one query-string in Oracle jdbc?

I have a JDBC connection to an Oracle database. I create a Statement. The SQL query String contains multiple statements separated by a semicolon, and is provided by a different system.
Example:
connection.prepareStatement("SELECT * FROM A; SELECT * FROM B");
According to ddimitrov it isn't possible.
But all other databases I've tried support it. And JDBC even has support to retrieve multiple results.
Does anyone have either pointers to Oracle documentation explicitly stating that it is not supported or have a way to make it work (without using of stored procedures)?
For executing multiple statements:
JDBC 2.0 lets you submit multiple statements at one time with the addBatch method
See here.
No, this is not possible with the Oracle JDBC driver.
You will have to parse and split the string into their individual statements.
Btw: I think the only databases that allow this are Microsoft SQL Server and MySQL. Which also makes them vulnerable to certain kind of SQL injection attacks that would not work Oracle or PostgreSQL.
AFAIK most databases only allow you to execute / prepare one statement per execute or prepare call. Although not very explicitly expressed, the intent of the JDBC methods is to execute a single SQL statement:
sql - **an** SQL statement that may [...]
The retrieval of multiple resultsets is for (very rare) single(!) statements or stored procedures which return multiple resultsets (as explained in the javadoc of Statement#execute).

Can you have a Database as a return type in a Java Method?

Can you have a Database as a return type in a Java method?
Right now I'm trying to write a method that takes in three databases as parameters, and takes the information from each each table, row by row, and combines it into 1 big database. The idea is simple but the implementation is fairing difficult. Any ideas?
What is a database?
The database is not part of your Java program (even if you think of embedded databases), from your program you only access the database through the JDBC.
With JDBC you get an SQLConnection and you can use it to lauch Statement or PreparedStatement. These statements provide you with a ResultSet (similar to a cursor in other languages) that you can use to get the different results.

Comparing sql query resultsets in java

I want to run a query programmatically using java on a sql server database running on our dev environment and production environment and compare the results.
would doing a column by column comparison be the best way to compare the resultsets or is there a better way to do it ?
Thank You
Comparing resultsets would work. The other option would be to create an object for each resultset and compare that. You could define your equals method to handle this, like any other ordinary Java object.
Much of this will depend on what you want to produce in the end. If all you're only concerned about resultset 1 being different from resultset 2, either method would work.
If you can do it in the database, do it in the database in a single query. It will be a lot easier to code, and will run a lot more faster.
Another quick test would be to query the data and dump it to a file then do a diff on the two files.

Categories

Resources