Preview sql result columns in java - java

Is there any way for 'previewing' sql select statements?
What I'm trying to do is to get the names of the columns that are returned by a sql statement without actually running the statement?
On application startup i need to know the column names, the problem is that some of the queries can run for awhile.

ResultSetMetaData may help
You still have to execute the query to get the meta data, but you may be able to alter add a restriction to the where clause which means it returns zero rows very quickly. For example you could append and 1 = 0 to the where clause.
The DBMS still has to do all the query parsing that it would normally do - it just means that the execution should hopefully fail very quickly

You didn't mention your DBMS, but the following works with the Postgres and Oracle JDBC drivers. I didn't test any other.
// the statement is only prepared, not executed!
PreparedStatement pstmt = con.prepareStatement("select * from foo");
ResultSetMetaData metaData = pstmt.getMetaData();
for (int i=1; i <= metaData.getColumnCount(); i++)
{
System.out.println(metaData.getColumnName(i));
}

Related

Why will Ucanaccess not delete my records?

I can connect to my access database and select, insert records etc. I am now trying to delete records and as far as I can see I am using the correct syntax. I have followed just about every tutorial I can find and they are not doing anything different that I can see.
String deleteSql = "DELETE FROM table1 WHERE sometext=? or sometext=?";
ps = module.getSupportConnection().prepareStatement(deleteSql);
ps.setString(1,"four");
ps.setString(2,"five");
int rs = ps.executeUpdate();
System.out.println(rs);
I have tried it without using int rs = .. but I used it just to see what the output was and it returns '2' which is what I was expecting as there are two records that meet the criteria used. It just wont delete the records and I cant see why. I dont get any errors when running the code. I appreciate this may not be a ucanaccess issue per se.

sql query returning 0 rows

I have this sql query which runs fine and return 3 rows when run on sql developer but when I execute the same query in a jsp page, it executes properly but doesn't return any rows. There is no problem with database connection because all other queries work fine.
Server - Tomcat 7
database - Oracle 10g
query -
select slno from lbk_tab where log_date = to_date('18-06-2017','DD-MM-YYYY')
jsp -
String dtol = "select slno from lbk_tab where log_date = to_date('18-06-2017','DD-MM-YYYY')";
Statement st = connection.createStatement();
ResultSet resultSet = st.executeQuery(dtol);
if (resultSet.next()) {
out.print(Integer.parseInt(resultSet.getString(1)));
}
Table lbk_tab has columns slno and log_date.
How can I fix this?
Try these things :
Classfiles are being generated afresh ? Clear & re-build project /
workspace.
Print the query and try to run printed query. Theoretically it looks the same from code, but just to be sure..
Check for query being fired at database also, may be java messes
with date object or date format. Hence the actual date fired from jsp says something else while fired at mysql points at something else ? debug / log / print query actually fired at mysql
end.
More clarity is required here, "to_date" referenced in query is
function ? what are column types.
I Think you need to use to_char()
select slno from lbk_tab where log_date = to_char(to_date('18-06-2017','DD-MM-YYYY'))

Prepared Statement insert into Multiple Tables using Try-with-resources , java DB and NetBeans 8.0 [duplicate]

I need to insert a couple hundreds of millions of records into the mysql db. I'm batch inserting it 1 million at a time. Please see my code below. It seems to be slow. Is there any way to optimize it?
try {
// Disable auto-commit
connection.setAutoCommit(false);
// Create a prepared statement
String sql = "INSERT INTO mytable (xxx), VALUES(?)";
PreparedStatement pstmt = connection.prepareStatement(sql);
Object[] vals=set.toArray();
for (int i=0; i<vals.length; i++) {
pstmt.setString(1, vals[i].toString());
pstmt.addBatch();
}
// Execute the batch
int [] updateCounts = pstmt.executeBatch();
System.out.append("inserted "+updateCounts.length);
I had a similar performance issue with mysql and solved it by setting the useServerPrepStmts and the rewriteBatchedStatements properties in the connection url.
Connection c = DriverManager.getConnection("jdbc:mysql://host:3306/db?useServerPrepStmts=false&rewriteBatchedStatements=true", "username", "password");
I'd like to expand on Bertil's answer, as I've been experimenting with the connection URL parameters.
rewriteBatchedStatements=true is the important parameter. useServerPrepStmts is already false by default, and even changing it to true doesn't make much difference in terms of batch insert performance.
Now I think is the time to write how rewriteBatchedStatements=true improves the performance so dramatically. It does so by rewriting of prepared statements for INSERT into multi-value inserts when executeBatch() (Source). That means that instead of sending the following n INSERT statements to the mysql server each time executeBatch() is called :
INSERT INTO X VALUES (A1,B1,C1)
INSERT INTO X VALUES (A2,B2,C2)
...
INSERT INTO X VALUES (An,Bn,Cn)
It would send a single INSERT statement :
INSERT INTO X VALUES (A1,B1,C1),(A2,B2,C2),...,(An,Bn,Cn)
You can observe it by toggling on the mysql logging (by SET global general_log = 1) which would log into a file each statement sent to the mysql server.
You can insert multiple rows with one insert statement, doing a few thousands at a time can greatly speed things up, that is, instead of doing e.g. 3 inserts of the form INSERT INTO tbl_name (a,b,c) VALUES(1,2,3); , you do INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(1,2,3),(1,2,3); (It might be JDBC .addBatch() does similar optimization now - though the mysql addBatch used to be entierly un-optimized and just issuing individual queries anyhow - I don't know if that's still the case with recent drivers)
If you really need speed, load your data from a comma separated file with LOAD DATA INFILE , we get around 7-8 times speedup doing that vs doing tens of millions of inserts.
If:
It's a new table, or the amount to be inserted is greater then the already inserted data
There are indexes on the table
You do not need other access to the table during the insert
Then ALTER TABLE tbl_name DISABLE KEYS can greatly improve the speed of your inserts. When you're done, run ALTER TABLE tbl_name ENABLE KEYS to start building the indexes, which can take a while, but not nearly as long as doing it for every insert.
You may try using DDBulkLoad object.
// Get a DDBulkLoad object
DDBulkLoad bulkLoad = DDBulkLoadFactory.getInstance(connection);
bulkLoad.setTableName(“mytable”);
bulkLoad.load(“data.csv”);
try {
// Disable auto-commit
connection.setAutoCommit(false);
int maxInsertBatch = 10000;
// Create a prepared statement
String sql = "INSERT INTO mytable (xxx), VALUES(?)";
PreparedStatement pstmt = connection.prepareStatement(sql);
Object[] vals=set.toArray();
int count = 1;
for (int i=0; i<vals.length; i++) {
pstmt.setString(1, vals[i].toString());
pstmt.addBatch();
if(count%maxInsertBatch == 0){
pstmt.executeBatch();
}
count++;
}
// Execute the batch
pstmt.executeBatch();
System.out.append("inserted "+count);

Setting work_mem in Postgres for specific queries

I'm using a library that delegates to a JDBC driver for PostgreSQL, and some queries are very complex and require more memory. I don't want to set work_mem to something large for all queries, just this subset. The problem is that executing the following code results in an error:
// pseudo-code for what is happening
String sql = "set work_mem = 102400;";
sql += "SELECT * FROM expensive_query";
ResultSet rs = DatabaseDriver.execute(sql);
When I run this I get an error that:
set work_mem = 102400;
returns no results.
This works in pgAdmin because you can execute multiple queries at once. Is there a better way to do this or do I need to execute arbitrary SQL and then extract the result set I want?
I have no idea what DatabaseDriver does, but with "plain" JDBC you just need to do the following:
Statment stmt = connection.createStatement();
stmt.execute("set work_mem = 102400");
ResultSet rs = stmt.executeQuery("select ...");
Try to do that using Batch Processing

get value from prepared statement query android?

i have a doubt that how to get the result of the prepared statement query in android.Actually i have a need that i want the row id from database while comparing a field words which can contain ' or" so i want to use prepared statement ,after googling out i did not get any proper example for android sqlite database ,please tell me how to use the prepared statement in android and after running query ,how to use value either through result set or through cursor.below is query look like-
String perfect_stmnt="select ID from Annotation where HighlightedWord=? ";
try{
** Connection con=DriverManager.getConnection("file:/"+ db.getPath());**
pstmt = con.prepareStatement(perfect_stmnt);
pstmt.setString(1, highlightword);
Resultset rs = pstmt.executeQuery();
Here the major doubt i am having how to get the connection here see the line having **
Thanks
The Android database API can prepare a statement (see compileStatement), but it is not possible to get a cursor or result set from that.
You can use compiled statements only if they return a single value, or nothing.
Please note that SQLite does not have a large overhead when preparing statements, so you can just call query or rawQuery multiple times.

Categories

Resources