I've read H2 docs about storing objects in database. There is special SQL type OTHER and methods setObject and getObject. I've tried this code:
PreparedStatement statement = null;
try {
statement = connection.prepareStatement("CREATE TABLE PUBLIC.foo (name VARCHAR(64) NOT NULL, data OTHER NULL);");
statement.execute();
} finally {
statement.close();
}
statement = null;
try {
statement = connection.prepareStatement("INSERT INTO PUBLIC.foo (name, data) VALUES(?,?);");
statement.setString(1, "lololo");
statement.setObject(2, new String[]{"foo", "bar"});
statement.execute();
}finally {
statement.close();
}
But I've got the exception:
org.h2.jdbc.JdbcSQLException: Ше�тнадцатирична� �трока �одержит неше�тнадцатиричные �имволы: "(foo, bar)"
Hexadecimal string contains non-hex character: "(foo, bar)"; SQL statement:
INSERT INTO PUBLIC.foo (name, data) VALUES(?,?) -- (?1, ?2) [90004-191]
What is wrong?
I believe this is what you were look for (Even I was).
You just need to create a column in your table with type as 'other'.
See 'create table testobj2(obj other)'
Look at my Sample code :
static String DB_DRIVER = "org.h2.Driver";
static String DB_CONNECTION = "jdbc:h2:./test2";
static String DB_USER = "";
static String DB_PASSWORD = "";
public static void benchmarkH2Inserts() {
try {
Class.forName(DB_DRIVER);
Connection dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
String createQry = "create table testobj2(obj other)";
String insertQuery = "insert into testobj2(obj) values(?)";
String selectQuery = "select * from testobj2";
// dbConnection.setAutoCommit(false);
dbConnection.prepareStatement(createQry).executeUpdate();
long lStartTime = System.nanoTime();
for(int i=0; i<10000; i++) {
dbConnection.setAutoCommit(false);
CloudElement_Circuit obj = new CloudElement_Circuit();
obj.setNrm8DesignId(1230L);
PreparedStatement preparedStatement = dbConnection.prepareStatement(insertQuery);
preparedStatement.setObject(1,obj);
preparedStatement.execute();
dbConnection.commit();
}
long lEndTime = System.nanoTime();
long output = lEndTime - lStartTime;
System.out.println("benchmarkH2Inserts() : Elapsed time in nanoseconds: " + output);
System.out.println("benchmarkH2Inserts() : Elapsed time in milliseconds: " + output / 1000000);
//Selecting
PreparedStatement preparedStatement = dbConnection.prepareStatement(selectQuery);
ResultSet rs = preparedStatement.executeQuery();
while(rs.next()) {
CloudElement_Circuit obj = (CloudElement_Circuit) rs.getObject("obj");
System.out.println("Fetched Object : " + obj.getNrm8DesignId());
}
dbConnection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Note that 'CloudElement_Circuit' is a Serialized class.
Look at 'OTHER Type' here : http://www.h2database.com/html/datatypes.html
H2 Example : https://www.javatips.net/blog/h2-database-example
Try this approach
List<String> genre = new ArrayList<String>();
String comma="";
StringBuilder allGenres = new StringBuilder();
for (String g: genre) {
allGenres.append(comma);
allGenres.append(g);
comma = ", ";
}
Then you can pass it like this
preparedStmt.setString (2, allGenres.toString());
Related
I'm trying to insert into a database, but with the added bonus of checking if there is duplicate data based on 5 fields.
For example I want to insert a line of data but for every ID, it will check 4 other fields, if all 5 fields match I do not want to insert. Other than that, an insert.
So far I have made the sql statement. I tried my query on a database dummy to see if it works and it does but when I run it in java with all the prepared statements, it gives me an error that says "Must declare the scalar variable". Here is my code and I do not know where to declare or what I must declare. Does it depend on the configuration of the database?
System.out.println("connection created successfully using properties file");
PreparedStatement pstmt2 = null;
PreparedStatement pstmt3 = null;
PreparedStatement pstmt5 = null;
PreparedStatement pstmt6 = null;
ResultSet rs = null;
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(
"C:\\Users\\darroyo\\Documents\\pruebasx.txt"));
} catch (FileNotFoundException e1) {
logger.error(e1.getMessage());
}
String line = null;
try {
line = reader.readLine();
} catch (IOException e1) {
logger.error(e1.getMessage());
}
String query = " insert into FRONTMC.HECHO (folio_hecho, folio_orden,"
+ "clave_sentido, titulos_hecho, precio, importe, liquidacion, contraparte, id_estatus, isin, contrato,"
+ "secondary_exec_id, exec_id, F11_ClOrdID, fecha_recepcion, fecha_sentra,emisora,serie)"
+ " select ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,convert(varchar(30),cast(? as datetime),120),convert(varchar(30),cast(? as datetime),120),?,?"
+" from FRONTMC.HECHO WHERE NOT EXISTS (SELECT * FROM FRONTMC.HECHO WHERE ISIN = ?"
+ "AND EMISORA = ? AND SERIE = ? AND CLAVE_SENTIDO = ? AND SECONDARY_EXEC_ID =?";
PreparedStatement preparedStmt = null;
try {
preparedStmt = con.prepareStatement(query);
} catch (SQLException e1) {
logger.error(e1.getMessage());
}
Map<Integer,String> hm1 = new HashMap<Integer,String>();
try {
do {
try{
String[] tokens = line.split("");
for (int i = 0; i != tokens.length; i++) {
int dataIndex = tokens[i].indexOf('=') + 1;
String data = tokens[i].substring(dataIndex);
hm1.put(new Integer(i),data);
}
String query2 = "select emisora from FRONTMC.EMISORA_CUSTODIO_SIGLO where isin = ?";
String query5 = " insert into FRONTMC.HECHO (emisora)"
+ " values ( ?)";
pstmt2 = con.prepareStatement(query2);
pstmt5 = con.prepareStatement(query5);
String query3 = "select serie from FRONTMC.EMISORA_CUSTODIO_SIGLO where isin = ?";
String query6 = " insert into FRONTMC.HECHO (emisora)"
+ " values ( ?)";
pstmt3 = con.prepareStatement(query3); // create a statement
pstmt6 =con.prepareStatement(query6);
setParameterString(preparedStmt,1, hm1.get(23));
setParameterString(preparedStmt,2, hm1.get(19));
setParameterString(preparedStmt,3, hm1.get(15));
setParameterString(preparedStmt,4, hm1.get(30));
setParameterString(preparedStmt,5, hm1.get(16));
setParameterString(preparedStmt,6, hm1.get(18));
setParameterString(preparedStmt,7, hm1.get(8));
setParameterString(preparedStmt,8, hm1.get(33));
setParameterString(preparedStmt,9, hm1.get(27));
setParameterString(preparedStmt,10, hm1.get(17));
setParameterString(preparedStmt,11, hm1.get(26));
setParameterString(preparedStmt,12, hm1.get(23));
setParameterString(preparedStmt,13, hm1.get(10));
setParameterString(preparedStmt,14, hm1.get(14));
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MM/yyyy");
String ds2 = null;
ds2 = sdf2.format(sdf1.parse(hm1.get(6)));
String newfecha1 = ds2;
setParameterString(preparedStmt,15, newfecha1);
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdf4 = new SimpleDateFormat("dd/MM/yyyy");
String ds4 = null;
ds4 = sdf4.format(sdf3.parse(hm1.get(6)));
String newfecha3 = ds4;
setParameterString(preparedStmt,16, newfecha3);
pstmt2.setString(1, hm1.get(17));
rs = pstmt2.executeQuery();
while (rs.next()) {
String emisora = rs.getString(1);
pstmt5.setString(1,emisora);
setParameterString(preparedStmt,17, emisora);
pstmt3.setString(1, hm1.get(17));
rs = pstmt3.executeQuery();
while (rs.next()) {
String serie = rs.getString(1);
pstmt6.setString(1,serie);
System.out.println(serie);
setParameterString(preparedStmt,18, serie);
setParameterString(preparedStmt,19, hm1.get(17));
setParameterString(preparedStmt,20, emisora);
setParameterString(preparedStmt,21, serie);
setParameterString(preparedStmt,22, hm1.get(15));
setParameterString(preparedStmt,23, hm1.get(23));
preparedStmt.execute();
}
}}catch(Exception ab){
new Thread(new Runnable(){
#Override
public void run(){
errorcon2();
}
}).start();
logger.error(ab.getMessage());
System.out.println(ab.getMessage());
ab.printStackTrace();
}
}while ((line = reader.readLine()) != null);}
catch(Exception a){
logger.error(a.getMessage());
}
new Thread(new Runnable(){
#Override
public void run(){
exitomsj();
}
}).start();
Here is the exception I get: Mistranslation it is not scalable, it is scalar.
com.microsoft.sqlserver.jdbc.SQLServerException: Debe declarar la variable escalar "#P18AND".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:217)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1655)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:440)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:385)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7505)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2445)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:166)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.execute(SQLServerPreparedStatement.java:367)
at swt.swtapp2$2.mouseDown(swtapp2.java:488)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:193)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4418)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4236)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3824)
at org.eclipse.jface.window.Window.runEventLoop(Window.java:818)
at org.eclipse.jface.window.Window.open(Window.java:794)
at swt.swtapp2.main(swtapp2.java:610)
You are missing a space.
+ "AND EMISORA = ? AND ...
should be
+ " AND EMISORA = ? AND ...
I want to update about 10K records into MySQL DB in less than a second. I have written below code which takes about 6-8 seconds to update a list of records into DB.
public void updateResultList(List<?> list) {
String user = "root";
String pass = "root";
String jdbcUrl = "jdbc:mysql://12.1.1.1/db_1?useSSL=false";
String driver = "com.mysql.jdbc.Driver";
PreparedStatement pstm = null;
try {
Class.forName(driver);
Connection myConn = DriverManager.getConnection(jdbcUrl, user, pass);
myConn.setAutoCommit(false);
for(int i=0; i<list.size(); i++) {
Object[] row = (Object[]) list.get(i);
int candidateID = Integer.valueOf(String.valueOf(row[0]));
String result = String.valueOf(row[14]);
int score = Integer.valueOf(String.valueOf(row[19]));
String uploadState = (String) row[20];
String sql = "UPDATE personal_info SET result = ?, score = ?, uploadState = ? "
+ " WHERE CandidateID = ?";
pstm = (PreparedStatement) myConn.prepareStatement(sql);
pstm.setString(1, result);
pstm.setInt(2, score);
pstm.setString(3, uploadState);
pstm.setInt(4, candidateID);
pstm.addBatch();
pstm.executeBatch();
}
myConn.commit();
myConn.setAutoCommit(true);
pstm.close();
myConn.close();
}
catch (Exception exc) {
exc.printStackTrace();
try {
throw new ServletException(exc);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Please let me know your inputs to optimize this code for performance improvement.
First, you need to init prepareStatement only once,you need to init it before the for loop
Second,you should avoid excute pstm.executeBatch(); for every loop it will cost much more resource,you need to execute it for a specified amount,such as 100,500 or more,also do not execute it outside the for loop for only once,due to it will cost more memory resource
Class.forName(driver);
Connection myConn = DriverManager.getConnection(jdbcUrl, user, pass);
myConn.setAutoCommit(false);
String sql = "UPDATE personal_info SET result = ?, score = ?, uploadState = ? "
+ " WHERE CandidateID = ?";
pstm = (PreparedStatement) myConn.prepareStatement(sql);
for(int i=0; i<list.size(); i++) {
Object[] row = (Object[]) list.get(i);
int candidateID = Integer.valueOf(String.valueOf(row[0]));
String result = String.valueOf(row[14]);
int score = Integer.valueOf(String.valueOf(row[19]));
String uploadState = (String) row[20];
pstm.setString(1, result);
pstm.setInt(2, score);
pstm.setString(3, uploadState);
pstm.setInt(4, candidateID);
pstm.addBatch();
if(i%500==0){//execute when it meet a specified amount
pstm.executeBatch();
}
}
pstm.executeBatch();
myConn.commit();
myConn.setAutoCommit(true);
Rather than batching the individual UPDATEs, you could batch INSERTs into a temporary table with rewriteBatchedStatements=true and then use a single UPDATE statement to update the main table. On my machine with a local MySQL instance, the following code takes about 2.5 seconds ...
long t0 = System.nanoTime();
conn.setAutoCommit(false);
String sql = null;
sql = "UPDATE personal_info SET result=?, score=?, uploadState=? WHERE CandidateID=?";
PreparedStatement ps = conn.prepareStatement(sql);
String tag = "X";
for (int i = 1; i <= 10000; i++) {
ps.setString(1, String.format("result_%s_%d", tag, i));
ps.setInt(2, 200000 + i);
ps.setString(3, String.format("state_%s_%d", tag, i));
ps.setInt(4, i);
ps.addBatch();
}
ps.executeBatch();
conn.commit();
System.out.printf("%d ms%n", (System.nanoTime() - t0) / 1000000);
... while this version takes about 1.3 seconds:
long t0 = System.nanoTime();
conn.setAutoCommit(false);
String sql = null;
Statement st = conn.createStatement();
st.execute("CREATE TEMPORARY TABLE tmp (CandidateID INT, result VARCHAR(255), score INT, uploadState VARCHAR(255))");
sql = "INSERT INTO tmp (result, score, uploadState, CandidateID) VALUES (?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
String tag = "Y";
for (int i = 1; i <= 10000; i++) {
ps.setString(1, String.format("result_%s_%d", tag, i));
ps.setInt(2, 400000 + i);
ps.setString(3, String.format("state_%s_%d", tag, i));
ps.setInt(4, i);
ps.addBatch();
}
ps.executeBatch();
sql =
"UPDATE personal_info pi INNER JOIN tmp ON tmp.CandidateID=pi.CandidateID "
+ "SET pi.result=tmp.result, pi.score=tmp.score, pi.uploadState=tmp.uploadState";
st.execute(sql);
conn.commit();
System.out.printf("%d ms%n", (System.nanoTime() - t0) / 1000000);
your pstm.executeBatch() should be after forloop
refer How to insert List into database
I am facing error java.sql.SQLFeatureNotSupportedException in my prepare statement. I am using Mysql database.
Below is my code.
class tmp {
public static void main(String arg[]) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/sample", "root", "root");
PreparedStatement pst = conn
.prepareStatement("select * from userinfo where firstname in(?)");
String[] Parameter = { "user1", "Administrator" };
Array sqlArray = conn.createArrayOf("VARCHAR", Parameter);
pst.setArray(1, sqlArray);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
For Mysql -
Setting array is not possible in Mysql.
Instead of that you can form a query for (?,?,..) in the loop and same way for setting values.
String[] Parameter = { "user1", "Administrator" };
String query = "select * from userinfo where firstname in (";
String temp = "";
for(i = 0; i < Parameter.length; i++) {
temp += ",?";
}
temp = temp.replaceFirst(",", "");
temp += ")";
query = query + temp;
PreparedStatement pst = conn.prepareStatement(query);
so query becomes
select * from userinfo where firstname in (?,?)
and pass values also using loop.
For Oracle -
ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor("CHAR_ARRAY", conn);
String[] Parameter = { "user1", "Administrator" };
java.sql.Array sqlArray = new oracle.sql.ARRAY(arrayDescriptor, conn, content);
.
.
pstmt.setArray(1, sqlArray);
Error message is very clear. And MySQL does not support custom data types.
Currently MySQL is supporting only:
Numeric Type
Date and Time Type
String Type
Or, you can use each of the input values as a set of values of IN function in MySQL.
Change your JAVA code as follows:
StringBuilder sbSql = new StringBuilder( 1024 );
sbSql.append( "select * from userinfo where firstname in(" );
for( int i=0; i < Parameter.length; i++ ) {
if( i > 0 ) sbSql.append( "," );
sbSql.append( " ?" );
} // for
sbSql.append( " )" );
PreparedStatement pst = conn.prepareStatement( sbSql.toString() );
for( int i=0; i < Parameter.length; i++ ) {
pst.setString( i+1, Parameter[ i ] );
} // for
ResultSet rs = pst.executeQuery();
Convert List to a comma separated String and use it.
Class Tmp {
public static void main(String arg[]) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/sample", "root", "root");
// Consider this list is already constructed
List<String> parameter = new ArrayList<String>();
parameter.add("user1");
parameter.add("Administrator");
String parameterStr = "'" + String.join("','", parameter) + "'";
PreparedStatement pst = conn.prepareStatement("select * from userinfo where firstname in(" + parameterStr + ")");
ResultSet rs = pst.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
I created a database with one table in MySQL:
CREATE DATABASE iac_enrollment_system;
USE iac_enrollment_system;
CREATE TABLE course(
course_code CHAR(7),
course_desc VARCHAR(255) NOT NULL,
course_chair VARCHAR(255),
PRIMARY KEY(course_code)
);
I tried to insert a record using Java:
// STEP 1: Import required packages
import java.sql.*;
import java.util.*;
public class SQLInsert {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/iac_enrollment_system";
// Database credentials
static final String USER = "root";
static final String PASS = "1234";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
Scanner scn = new Scanner(System.in);
String course_code = null, course_desc = null, course_chair = null;
try {
// STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// STEP 3: Open a connection
System.out.print("\nConnecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println(" SUCCESS!\n");
// STEP 4: Ask for user input
System.out.print("Enter course code: ");
course_code = scn.nextLine();
System.out.print("Enter course description: ");
course_desc = scn.nextLine();
System.out.print("Enter course chair: ");
course_chair = scn.nextLine();
// STEP 5: Excute query
System.out.print("\nInserting records into table...");
stmt = conn.createStatement();
String sql = "INSERT INTO course " +
"VALUES (course_code, course_desc, course_chair)";
stmt.executeUpdate(sql);
System.out.println(" SUCCESS!\n");
} catch(SQLException se) {
se.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if(stmt != null)
conn.close();
} catch(SQLException se) {
}
try {
if(conn != null)
conn.close();
} catch(SQLException se) {
se.printStackTrace();
}
}
System.out.println("Thank you for your patronage!");
}
}
The output appears to return successfully:
But when I select from MySQL, the inserted record is blank:
Why is it inserting a blank record?
no that cannot work(not with real data):
String sql = "INSERT INTO course " +
"VALUES (course_code, course_desc, course_chair)";
stmt.executeUpdate(sql);
change it to:
String sql = "INSERT INTO course (course_code, course_desc, course_chair)" +
"VALUES (?, ?, ?)";
Create a PreparedStatment with that sql and insert the values with index:
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, "Test");
preparedStatement.setString(2, "Test2");
preparedStatement.setString(3, "Test3");
preparedStatement.executeUpdate();
this can also be done like this if you don't want to use prepared statements.
String sql = "INSERT INTO course(course_code,course_desc,course_chair)"+"VALUES('"+course_code+"','"+course_desc+"','"+course_chair+"');"
Why it didnt insert value is because you were not providing values, but you were providing names of variables that you have used.
This should work for any table, instead of hard-coding the columns.
//Source details
String sourceUrl = "jdbc:oracle:thin:#//server:1521/db";
String sourceUserName = "src";
String sourcePassword = "***";
// Destination details
String destinationUserName = "dest";
String destinationPassword = "***";
String destinationUrl = "jdbc:mysql://server:3306/db";
Connection srcConnection = getSourceConnection(sourceUrl, sourceUserName, sourcePassword);
Connection destConnection = getDestinationConnection(destinationUrl, destinationUserName, destinationPassword);
PreparedStatement sourceStatement = srcConnection.prepareStatement("SELECT * FROM src_table ");
ResultSet rs = sourceStatement.executeQuery();
rs.setFetchSize(1000); // not needed
ResultSetMetaData meta = rs.getMetaData();
List<String> columns = new ArrayList<>();
for (int i = 1; i <= meta.getColumnCount(); i++)
columns.add(meta.getColumnName(i));
try (PreparedStatement destStatement = destConnection.prepareStatement(
"INSERT INTO dest_table ("
+ columns.stream().collect(Collectors.joining(", "))
+ ") VALUES ("
+ columns.stream().map(c -> "?").collect(Collectors.joining(", "))
+ ")"
)
)
{
int count = 0;
while (rs.next()) {
for (int i = 1; i <= meta.getColumnCount(); i++) {
destStatement.setObject(i, rs.getObject(i));
}
destStatement.addBatch();
count++;
}
destStatement.executeBatch(); // you will see all the rows in dest once this statement is executed
System.out.println("done " + count);
}
There is a mistake in your insert statement chage it to below and try :
String sql = "insert into table_name values ('" + Col1 +"','" + Col2 + "','" + Col3 + "')";
I have two arraylists to insert into 2 columns in a database table as follows:
arraylist1: 123444, 324555, 6423643, 532326
arraylist2: jkfdsl, nkjfsdlj, jdslkfjdlkj, jfsldjfsk, fjdlskjfs
I wrote the following code to insert the arraylists but it is not working. I will appreciate your help.
try {
// Prepare a statement to insert a record
String sql = "INSERT INTO soundsdata.splog (arraylist1, arraylist2) VALUES(?,?)";
pstmt = (PreparedStatement) con.prepareStatement(sql);
pstmt.setArray(1,sptospring);
pstmt.setString(2,eachList.toString());
// Insert the row
pstmt.executeUpdate();
}finally {
pstmt.close();
}
Here's something that you can do:
Assuming that you're trying to create one row, where the 1st column will contain the content of the first ArrayList in comma-separated format and the 2nd column will contain the content of the secondArrayList
StringBuilder buffer = new StringBuilder();
boolean processedFirst = false;
String firstParam = null, secondParam = null;
try{
for(String record: arrayList1){
if(processedFirst)
buffer.append(",");
buffer.append(record);
processedFirst = true;
}
firstParam = buffer.toString();
}finally{
buffer = null;
}
processedFirst = false;
buffer = new StringBuilder();
try{
for(String record: arrayList2){
if(processedFirst)
buffer.append(",");
buffer.append(record);
processedFirst = true;
}
secondParam = buffer.toString();
}finally{
buffer = null;
}
secondParam = buffer.toString();
String sql = "INSERT INTO soundsdata.splog (arraylist1, arraylist2) VALUES(?,?)";
try{
psmt = (PreparedStatement) con.prepareStatement(sql);
pstmt.setString(1,firstParam);
pstmt.setString(2,secondParam);
pstmt.executeUpdate();
}finally {
pstmt.close();
}
You cannot store an ArrayList in a varchar column.
You need to store a string.
PreparedStatement ps = connection.prepareStatement(query);
for (Record record : arraylist1) {
int index=1;
ps.setString(index++,record.getItem());
ps.setString(index++,record.getItem2());
//
}
ps.executeBatch();
conn.commit();
Insert more than one record:
public String saveOrder(ArrayList<KIT0053MBean> insertList){
System.out.println("SaveOrder DAO Method is calling " +insertList.size());
Connection con=null;
PreparedStatement ps2=null;
try {
con=open();
con.setAutoCommit(false);
con.getTransactionIsolation();
ps2=con.prepareStatement(sql1);
Iterator<KIT0053MBean> it=insertList.iterator();
while(it.hasNext()){
KIT0053MBean order=(KIT0053MBean)it.next();
ps2.setString(1, model.getCustomerid());
ps2.setString(2, model.getSerialid());
ps2.addBatch();
}
int i[]=ps2.executeBatch();
System.out.println("###### insertion1### row "+i.length);
con.commit();
con.setAutoCommit(true);
} catch (Exception e)
{
System.out.println(e.getMessage());
}finally{
close(con);
close(ps2);
}
}
String[] stringArray = lists.toArray(new String[lists.size()]);
String string1 = stringArray[0];
String string2 = stringArray[1];
String string3 = stringArray[2];
String string4 = stringArray[3];
String string5 = stringArray[4];
// then write query for insert into database
insert into tablename values(string1 ......)