So, I'm trying to insert data to a database completely dynamically, meaning the data will be inserted without any knowledge of what table we're inserting into. The same goes for the different attributes.
My problem is that for some reason, my table names get wrapped in '' when preparing the statment. It goes as follows:
String query = "INSERT INTO ? VALUES(?)";
try{
Connection conn = Connection.create();
PreparedStatment st = conn.prepareStatement(query);
st.setString(1, "test");
st.setString(2, "1");
st.addBatch();
//... (executes batch later on...)
catch (Exception e){
e.printStackTrace();
}
finally{
DbUtils.closeQuietly(rs);
DbUtils.closeQuietly(conn);
}
Now for some reason, and completely beyond me, if I print this statement I get:
"INSERT INTO 'test' VALUES("1");
while I would expect
"INSERT INTO test VALUES("1");
Could anyone explain why this happens and how I can solve this, or a better way to achieve what I'm trying to do? If it matters I'm using DbUtils to handle closing, and 3CP0 for connection pooling.
I have been looking all over without any luck. And I would also appreciate it if anyone could tell me if inserting data dynamically to different tables generally is a bad thing.
Related
I want to insert some data into RDBMS(MYSQL5.7, JDBC5.1), and I am using executeBatch to speed up. I found that when one insert sql of a batch of statement cannot execute properly, the remain will be ignored. I have to say that not what I expectd. I want to let RDBMS just ignore my error sql and continue execution.
Can anyone tell me what should I do or show me the right documentation url.
And I am glad for your help!
And I know that "INSERT ... ON DUPLICATE KEY ... " can help ignore DUPLICATE KEY error , but I want one more general solution.
MySQL JDBC DOC
Connection connection = getConn();
Statement statemenet = connection.createStatement();
for (SearchUserItem user: result.getItems()) {
String query = "INSERT INTO TABLE VALUES(SOME_VALUES)";
}
int[] re = statemenet.executeBatch();
ananlyseBatchResult(re);
statemenet.close();
connection.close();
I have written below program to achieve this:
try {
PreparedStatement statement = connection.prepareStatement(
"SELECT * FROM some_table some_timestamp<?)");
statement.setTimestamp(1, new java.sql.Timestamp(dt.getTime()));
ResultSet resultSet = statement.executeQuery();
CSVWriter csvWriter = new CSVWriter(new FileWriter(activeDirectory + "/archive_data" + timeStamp + ".csv"), ',');
csvWriter.writeAll(resultSet, true);
csvWriter.flush();
} catch (Exception e) {
e.printStackTrace();
}
// delete from table
try {
PreparedStatement statement = connection.prepareStatement(
"DELETE FROM some_table some_timestamp<?)");
statement.setTimestamp(1, new java.sql.Timestamp(dt.getTime()));
statement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}
dbUtil.close(connection);
Above program would just work fine for an average scenario but I would like to know how can I improve this program which:
Works smoothly for a million records without overloading the application server
Considering there would be many records getting inserted into the same table at the time this program runs, how can I ensure this program archives and then purges the exact same records.
Update: I m using openscv http://opencsv.sourceforge.net/
I would like to suggest several things:
refrain from using time as limit point. It can be cause
unpredictable bugs. Time can be different in different places and
different environment so we should be careful with time. Instead
time use sequence
Use connection pool to get data from database
Save information from db in different files. You can store them on
different drives. After that you have to concatenate information
from them.
Use memory mapped files.
Use multi-threaded model for getting and storing/restoring
information. Note: JDBC doens't support many threads so connection
pool is your helper
And these steps are only about java part. You need to have good design on your DB side. Not easy, right? But this is price for using large data.
I want to convert the records in the JDBC resultset into insert queries for some purposes.
Is it possible? If it is pls suggest me the solution.
Here is a question about generating CREATE TABLE query from ResultSet.
How to create table based on JDBC Result Set
With some changes you should be able to adapt it for an INSERT query also.
Note that, the values have to be added with quotes or with date conversion function etc. as per the data type of the column.
Also, large objects such as CLOB would require some additional bit of work to make them work.
You can use pure SQL for this.
As MySQL docs says:
INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;
You can use a select subquery with an insert in other SQL dialects also.
Based on my understanding on your quesstion, You can try like this
try{
ps=con.prepareStatement("select * from login");
rs=ps.executeQuery();
while(rs.next())
{
ps=con.prepareStatement("insert into table2(uname,pswd) values(?,?)");
ps.setString(1,rs.getString(2));// 2-column number
ps.setString(2,rs.getString(3));//3-column number
ps.executeUpdate();
}
}catch(Exception e)
{
System.out.println(e);
}
I'm developing a web application with Play 2.1.0 and programming it with Java and I need to have access to data already saved in a DB to modify them.
I tried to create a new instance without the new operator and reference it to my object saved in the database, but even if there is no pointer error, it won't change values of attributes. I couldn't figure out why, so I've decided to enter SQL queries directly.
Same thing, it does not seems to have any mistake, but it won't change anything... I think this comes from a bad link to the database :
Here is my code in application.java :
public static Result modifyQuestionnaire(Long id) throws SQLException {
Statement stmt = null;
Connection con = DB.getConnection();
try {
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String query = "SELECT * FROM WOQ.questionnaire WHERE id=id";
ResultSet uprs = stmt.executeQuery(query);
uprs.updateString("name", "baba");
uprs.updateRow();
} catch(Exception e) {
e.printStackTrace();
} finally {
if (stmt!=null) {
stmt.close();
}
}
return redirect(routes.Application.questionnaire(id));
}
And I also try to enter an UPDATE query directly, still the same..
I've looked everywhere and did not find any solution (except Anorm but it seems to work with Scala language)
Btw, if anyone knows a solution with a second instance that refers to the same object (it seems possible but as I say, there is no error but no actions neither), it's fine for me.
Huh, you showed as that you are trying to create totally new connection, so I supposed, that you don't want to use Ebean, but in case when you are already use it, you can just use its methods for the task:
(copied) There are some options in Ebean's API, so you should check it and choose one:
Update<T> - check in the sample for #NamedUpdates annotation
Ebean.createUpdate(beanType, updStatement)
SqlUpdate - you can just perform raw SQL update, without need for giving the entity type
Started coming up with a java web app for online user interaction. Decided to use a MySql DB for data storage. I have already created the tables with the proper/expected data types. My question is I always thought the next step would be to creat stored procedures like Search/Add/Delete/etc.. that the user could envoke from the page. So in my java code I could just call the procedure ex:
CallableStatement cs;
Try
{
String outParam = cs.getString(1); // OUT parameter
// Call a procedure with one in and out parameter
cs = connection.prepareCall("{call SearchIt(?)}");
cs.registerOutParameter(1, Types.VARCHAR);
cs.setString(1, "a string");
cs.execute();
outParam = cs.getString(1);
}
catch (SQLException e) {
}
but if my application was not in the need for stored procedures because the user actions would be simple enough to execute simple tedious queries. How could I set up my Java and Sql code to handle that. Could I just have the "Select" or "Update" statements in my code to manipulate the data in my MySQL DB. If so how would that syntax look like?
This URL has documentation on using prepared statements which is what you want to use to avoid security flaws (SQL Injection and such).
http://download.oracle.com/javase/tutorial/jdbc/basics/prepared.html
here's an example from that page
PreparedStatement updateSales = connection.prepareStatement(
"UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ? ");
updateSales.setInt(1, 75);
updateSales.setString(2, "Colombian");
updateSales.executeUpdate():
Just use Statement, or PreparedStatement.
http://download.oracle.com/javase/1.4.2/docs/api/java/sql/Statement.html
In a similar way to what you did, just call :
Statement stm = Connection.createStatement();
then execute your SQL :
stm.execute("SELECT * FROM MYTABLE");
grab the resultset and check out the results.
Beware though - this is bad bad as far as security goes - as others have mentioned, PreparedStatements are a bit more secure, but still not 100%.
To be honest, although basic JDBC is pretty simple, I really hate all the SQL strings littered around your code. If you want something a bit more elegant have a quick look at hibernate - it hides all the hackiness from you, and is also pretty easy to setup.