Comparing sql query resultsets in java - 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.

Related

How to receive all ResultSets from Statement from JDBC?

I execute the following queries:
SELECT 1; CREATE TABLE ....; SELECT 2;
after that I try to get all resultSets. I use the following code for this:
while(!stmt.getMoreResults()) {
stmt.getResultSet();
}
Unfortunately I get jus first result set. Please tell me what I'm doing wrong?
Your second CREATE TABLE statement technically does not even return a result set (though JDBC might return a count of the records affected by DML). So, if you want to capture the conceptual return value of each statement, then you should just run them separately.
If your second statement were actually a SELECT, then perhaps we could find some way to combine the queries together.
Read this canonical answer to see how to handle the case where you really do have mulitple selects. But, note that not all databases support this (e.g. Oracle does not support it). And read here to see why multiple queries in a single JDBC call might even be a bad thing.
Are you trying to say that Java is just not capable of doing the thing .Net does without a hitch? Is it as simple as that?
No matter what kind of dummy statements are between selects in the script that is run as batch the bool IDataReader.NextResult() in the C# code reliably returns next result jumping over the next dummy statements for Netezza we are trying to debug for now. It did the same thing for many years for all the platforms that support batch calls in case we had to deal with them.

Is there any use for views,triggers and stored procedures for a Java GUI project?

I am making a Java gui and web application which will use the same mysql database.
It's a DTh management system where all the information will be stored and retrieved dynamically depending on input.
I believe that views are static by nature and thus would be useless as all my queries will have a different where condition (userid).
Do I need to use triggers? I mean I could code the java to execute multiple statements instead of using a inbuilt trigger (e.g. Insert in customers name and family members name both will have a duplicate copy for head of the family). Is there a performance hit? Am I wrong in some way?
And same thing what is the use of stored procedures? Can't I use methods in java to do everything?
So, I am asking is it possible to shift all the calculation intensive stuff to java and web script instead of the sql. If yes, does this mean I only have to create the backend structure of Database(i.e. all the different tables and FK,PK) and do rest without using any sql stuff on mysql workbench?
Thank you for helping.
There is (as always) one correct answer: It depends.
If you only want to show and query some data, you probably won't need trigger or stored procedures.
Views are a different thing: They are pretty helpful if you want a static viesw to a join-table or something like that. If you don't need this, just don't use it.
Keys are really important. They make your data robust against wrong input.
What you shoud use is PrepearedStatement instead of Statement. If you only use PreparedStatements, you are (nearly ?) safe in the question of SQL-Injection.
We use Views because it just faster than select query and for just showing data (not edit-update) it is faster and preferable.
Trigger are fired at database side so it is faster because it just execute 2 or more queries in single execution.
Same in Stored procedures, because we can execute more than one queries in single database connection. If we execute different queries than it take more time on every execution for database connection (find database server, authenticate, find database,... etc.).

Java, prepareStatement, SQL_CALC_FOUND_ROWS, FoundRows() hows it work

I'm trying to do pagination from the db and so far I have found that if you want to use limit you should use SQL_CALC_FOUND_ROWS.
However, in Java I'm having some issues understanding how to call this.
Can I do it in one prepareStatement or will I need to make multiple calls? If im making multiple calls, how does MySQL know I'm referring to the last call for the count?
Thanks.
You'll have to make two calls (unless you implement a some stored function in the db which returns both the limited resultset and the count, which I assume is possible in mysql but never tried that).
MySQL will know what query counts to return because both the query with the limit and the SELECT FOUND_ROWS(); will be in the same session.

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.

MySQL Prepared Statements vs Stored Procedures Performance

I have an old MySQL 4.1 database with a table that has a few millions rows and an old Java application that connects to this database and returns several thousand rows from this this table on a frequent basis via a simple SQL query (i.e. SELECT * FROM people WHERE first_name = 'Bob'. I think the Java application uses client side prepared statements but was looking at switching this to the server, and in the example mentioned the value for first_name will vary depending on what the user enters).
I would like to speed up performance on the select query and was wondering if I should switch to Prepared Statements or Stored Procedures. Is there a general rule of thumb of what is quicker/less resource intensive (or if a combination of both is better)
You do have an index of first_name, right? That will speed up your query a lot more than choosing between prepared statements and stored procedures.
If you have just one query to worry about, you should be able to implement the two alternatives (on your test platform of course!) and see which one gives you the best performance.
(My guess is that there won't be much difference though ...)
Looks like the best way is just to make the change and test it out in a test environment.
Thanks for the help.

Categories

Resources