How to print used SQL query when using PreparedStatement (JDBC) - java

I used PreparedStatement in my Java program. I need to debug the SQL query because it's not working fine.
Is there is away to print the used SQL statement with the values inserted
for example in PreparedStatement:
select * from table where a=?
and than I set the ?
could I print the used SQL, for example:
select * from table where a=1

It can't be done with java.sql.PreparedStatement interface itself; it depends on the implementation by your database vendor.
But you're in luck; the MySQL driver allows you to do it using its toString implementation:
http://www.avajava.com/tutorials/lessons/how-do-i-display-a-prepared-statement-with-bind-variables-using-mysql.html
You need to be aware that using this vendor-specific feature binds your code to MySQL. You can't change databases without changing your code.

Related

How to use the SQL RANDOM() function in a Netbeans JavaDB

I need to do a SQL call in a small local JavaDB in Netbeans 7.2 to pull a single random row from the database.
SELECT * FROM JAVA2.FORTUNES ORDER BY RANDOM()
So far, I've got it to work using the RANDOM() function, but I'm having trouble getting LIMIT 1 to work, it returns a syntax error.
I know that every database has a different way to do this, and I can't figure out how it works specifically for this JavaDB in Netbeans (I got it to work in a separate Oracle DB with different syntax).
Is there a Java DB specific, or ANSII standard way to return a single row using the above syntax?
Java DB uses SQL's OFFSET/FETCH syntax to get this functionality.
Assuming RANDOM() orders them correctly the following syntax should work:
SELECT * FROM JAVA2.FORTUNES ORDER BY RANDOM() OFFSET 0 ROWS FETCH NEXT 1 ROW ONLY
From http://db.apache.org/derby/faq.html#limit
Derby does not support the LIMIT syntax. However, Derby 10.4 added the
ROW_NUMBER function and Derby 10.7 added the OFFSET and FETCH clauses.
Derby also supports limiting the number of rows returned by a query
through JDBC. For example, to fetch the first 5 rows of a large table:
Statement stmt = con.createStatement();
stmt.setMaxRows(5);
ResultSet rs = stmt.executeQuery("SELECT * FROM myLargeTable");

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.

Memcache implementation design

Iam trying to implement memcache in my web application and just wanted to get suggestions that whether what iam doing is right in terms of design.
I have a class SimpleDataAccessor which run all my insert, update and select sql queries. So any query that has to be performed is executed inside the method of this class.
So inside the method where I have my select query implementation i have a method which stores the resultset in memcache like this.
storeinMC(resultset.getJSON(),sqlquery);
the sqlquery here is my key.
Also before running the selectquery i check in memcache that whether I have a resultset already for that query.
if((String res=getRSFromMC(sqlquery)==null)
So i've tried to keep it plain and simple.
Do you see any issues with this.?
As rai.skumar rightfully pointed out your SQL statements could be constructed differently (e.g. WHERE clause could contain same conditions in diff order, etc.)
So to overcome above mentioned issues, you need to parse your SQL and get all the relevant pieces from it. Then you can combine these pieces into a cache key.
You can take a look at SQL parsers: ZQL, JSqlParser, General SQL Parser for Java that return you java classes out of your SQL.
Another option would be to use JPA instead of straight JDBC. For example Hibernate has great JPA support and fully capable of caching your queries.
If you feel closer to JDBC you could use MyBatis that has very JDBC like syntax and caching support.
Consider below queries:
String k1 = "Select * from table"; //Query1
String k2 = "Select * from TABLE"; // Query2 ; notice TABLE is in caps
Both of above SQL queries are same and will fetch same data. But if above queries are used as keys in Memchached they will get stored at different places ( as k1.equals(k2) will return false).
Also if somehow you can ensure that there are no typos or extra spaces, it won't be very efficient as keys/queries could be very big.

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).

how to get insert sql in mysql shell or java library?

my table is:
[tableAbc]
A B C
------------
1 a b
2 c e
3 e r
...
run:
show create table tableAbc;
to get the create table sql
but how to get insert sql?
like:
insert into `tableAbc` ('A','B','C') VALUES(1,'a','b');
...
any idea? (or any java library to do this)
thanks all!
BTW:
i want show the "insert sql" to web brower.
so,i think i need get "insert sql" under java, or sql commands.
If you use Squirrel (A SQL tool) it can generate this for you.
I use this tool daily (it is free and it works well)
As per their description
SQuirreL SQL Client is a graphical
Java program that will allow you to
view the structure of a JDBC compliant
database, browse the data in tables,
issue SQL commands etc,
To get the insert statement for a table just right click on a table and select "data scripts"
Your question is difficult to understand due to everything being in sentence-fragments, but I think you're basically just looking for a way to be able to send SQL statements to your SQL database from Java, specifically an insert statement?
If this is the case, Java has an sql package. You can create a connection to your database, create a statement, and execute it. This will then return a ResultSet which is the result of the executed statement. Start here perhaps? http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Connection.html#createStatement%28%29 And there is part of an example on the documentation for ResultSet http://java.sun.com/j2se/1.4.2/docs/api/java/sql/ResultSet.html
What do you mean by "i want to show the insert sql in the browser"? Do you mean you want to show what the result of the executed statement is?

Categories

Resources