PostgreSQL: How to insert data into database table with loop? - java

Hello fellow Stackoverflowers!
I am trying to INSERT INTO a database table 'dotcom' 4 columns of 5000 rows of data from a text file in the form [x, w, x, y, z] e.g.
1 google com null null
2 google co uk null
...
and the rows returned remains '1' instead of '5000' for some reason.
The problem is that the rows are not inserted into the database table even though the BufferedReader reads it. How may I solve this problem? E.g. inserting the data into the table with a loop?
Any help would be greatly appreciated!
import java.sql.*;
import java.util.Scanner;
import java.io.*;
public class Database {
public static Connection connectToDatabase(String user, String port,
String database) {
System.out.println("-------- PostgreSQL JDBC Connection Testing ------------");
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your PostgreSQL JDBC Driver? " + "Include in your library path!");
e.printStackTrace();
}
System.out.println("PostgreSQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:postgresql://localhost:" + port + "/" + database, user, "doesn't matter!");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
}
return connection;
}
public static ResultSet executeSelect(Connection connection, String query) {
Statement st = null;
try {
st = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
ResultSet rs = null;
try {
rs = st.executeQuery(query);
//st.close();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return rs;
}
public static void dropTable(Connection connection, String table) {
Statement st = null;
try {
st = connection.createStatement();
st.execute("DROP TABLE " + table);
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void createTable(Connection connection,
String tableDescription) {
Statement st = null;
try {
st = connection.createStatement();
st.execute("CREATE TABLE " + tableDescription);
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static int insertIntoTableFromFile(Connection connection,
String table, String file) {
BufferedReader br = null;
int numRows = 0;
try {
Statement st = connection.createStatement();
String sCurrentLine, brokenLine[], composedLine = "";
br = new BufferedReader(new FileReader("src/TopURLs"));
while ((sCurrentLine = br.readLine()) != null) {
// Insert each line to the DB
brokenLine = sCurrentLine.split("\t");
composedLine = "INSERT INTO dotcom VALUES (";
int i;
for (i = 0; i < brokenLine.length - 1; i++) {
composedLine += "'" + brokenLine[i] + "',";
}
composedLine += "'" + brokenLine[i] + "')";
numRows = st.executeUpdate(composedLine);
//System.out.println(composedLine);
}
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return numRows;
}
public static void main(String[] argv) throws SQLException {
/*
Scanner input = new Scanner(System.in);
System.out.println("Please enter your Username:");
String user = input.next();
System.out.println("Please enter your Port ID:");
String port = input.next();
*/
String user = "zbva777";
String port = "28046";
String database = "test";
Connection connection = connectToDatabase(user, port, database);
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
return;
}
// Now we're ready to work on the DB
String query = "SELECT * FROM dotcom";
ResultSet rs = executeSelect(connection, query);
try {
while (rs.next()) {
System.out.print("Column 1 returned ");
System.out.println(rs.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
rs.close();
dropTable(connection, "dotcom");
createTable(connection, "dotcom (rank int primary key, name varchar(5000), type varchar(5000), subtype varchar(5000), subsubtype varchar(5000));");
int rows = insertIntoTableFromFile(connection, "dotcom", "src/TopURLs");
System.out.println(rows + " rows inserted.");
}
}

In each iteration of the loop you override numRows instead of incrementing it with the newly added row. Just replace the = with += and you should be OK:
numRows += st.executeUpdate(composedLine);
// Here ^
That being said, you should really look into PreparedStatements and executing batches.

Related

Compare csv file with MySQL database

I am creating a program in Java and I need to compare if data in a csv file is exactly the same that exists on a mysql table?
How can i do that?
For example, i have a table "Suplyers" with columns "Id, Name and Adress".
Thanks
Below is the code that i have that read csv file and that connect to database and shows the data in the table.
public static void le_csv() {
String row;
BufferedReader csvReader = null;
try {
csvReader = new BufferedReader(new FileReader("C:\\Users\\User\\Desktop\\ficheiros\\fornecedores.csv"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while ((row = csvReader.readLine()) != null) {
String[] data = row.split(",");
System.out.println(data[0] + "\t" + data[1] + "\t" + data[2]);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
csvReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
>
>
>
public static void query(){
try {
String url = "jdbc:mysql://127.0.0.1:3306/database";
String user = "user";
String password = "password";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, user, password);
String sql = "SELECT * FROM SUPLYERS";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString(1) + "\t" + rs.getString(2) + "\t" + rs.getString(3);
}
rs.close();
st.close();
conn.close();
} catch (Exception exc) {
exc.printStackTrace();
}
}
You may collect the CSV data and DB data into lists of String and then compare the lists using equals:
public static List<String> readCsvData() {
List<String> csvData = new ArrayList<>();
// use try-with-resources to auto close reader
try (BufferedReader csvReader = new BufferedReader(new FileReader("C:\\Users\\User\\Desktop\\ficheiros\\fornecedores.csv"))){
String row;
while ((row = csvReader.readLine()) != null) {
String[] data = row.split(",");
row = String.join("\t", data[0], data[1], data[2]);
System.out.println(row);
csvData.add(row);
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return csvData;
}
public static void initDb() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static List<String> readDbData(){
initDb();
String url = "jdbc:mysql://127.0.0.1:3306/database";
String user = "user";
String password = "password";
String sql = "SELECT * FROM SUPLYERS";
List<String> dbData = new ArrayList<>();
// use try-with-resources to auto close SQL connection, etc.
try (Connection conn = DriverManager.getConnection(url, user, password);
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
) {
while (rs.next()) {
String row = String.join("\t", rs.getString(1), rs.getString(2), rs.getString(3));
System.out.println(row);
dbData.add(row);
}
} catch (Exception exc) {
exc.printStackTrace();
throw new RuntimeException(exc);
}
return dbData;
}
public static boolean areCsvDataSameAsDb() {
List<String> csvData = readCsvData();
List<String> dbData = readDbData();
return csvData.equals(dbData);
}
Or you can read the data row by row to shortcut the check as soon as any discrepancy is detected.

PostgreSQL: How to print out ResultSet?

Hello fellow Stackoverflowers!
How do you print out a ResultSet's column e.g. 'name' (in order of number rank)?
I am stuck at the bottom of the main method # //Print data from result set
Thank you for your help!
import java.sql.*;
import java.util.Scanner;
import java.io.*;
public class Database {
public static Connection connectToDatabase(String user, String port,
String database) {
System.out.println("-------- PostgreSQL JDBC Connection Testing ------------");
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your PostgreSQL JDBC Driver? " + "Include in your library path!");
e.printStackTrace();
}
System.out.println("PostgreSQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:postgresql://localhost:" + port + "/" + database, user, "doesn't matter!");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
}
return connection;
}
public static ResultSet executeSelect(Connection connection, String query) {
Statement st = null;
try {
st = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
ResultSet rs = null;
try {
rs = st.executeQuery(query);
//st.close();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return rs;
}
public static void dropTable(Connection connection, String table) {
Statement st = null;
try {
st = connection.createStatement();
st.execute("DROP TABLE " + table);
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void createTable(Connection connection,
String tableDescription) {
Statement st = null;
try {
st = connection.createStatement();
st.execute("CREATE TABLE " + tableDescription);
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static int insertIntoTableFromFile(Connection connection,
String table, String file) {
BufferedReader br = null;
int numRows = 0;
try {
Statement st = connection.createStatement();
String sCurrentLine, brokenLine[], composedLine = "";
br = new BufferedReader(new FileReader("src/TopURLs"));
while ((sCurrentLine = br.readLine()) != null) {
// Insert each line to the DB
brokenLine = sCurrentLine.split("\t");
composedLine = "INSERT INTO dotcom VALUES (";
int i;
for (i = 0; i < brokenLine.length - 1; i++) {
composedLine += "'" + brokenLine[i] + "',";
}
composedLine += "'" + brokenLine[i] + "')";
numRows += st.executeUpdate(composedLine);
//System.out.println(composedLine);
}
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return numRows;
}
public static void print() {
System.out.println("########## 1st Query ##########");
}
public static void main(String[] argv) throws SQLException {
/*
Scanner input = new Scanner(System.in);
System.out.println("Please enter your Username:");
String user = input.next();
System.out.println("Please enter your Port ID:");
String port = input.next();
*/
String user = "zbva777";
String port = "28046";
String database = "test";
Connection connection = connectToDatabase(user, port, database);
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
return;
}
// Now we're ready to work on the DB
// connection is of type Connection (in JDBC)
DatabaseMetaData dbm = connection.getMetaData();
// check if table is there
ResultSet tables = dbm.getTables(null, null, "dotcom", null);
if (tables.next()) {
System.out.println("Table exists");
} else {
System.out.println("Table does not exist");
}
// check if view is there?
//"create view foo as select * from table;"
//"select * from foo;"
ResultSet views = dbm.getTables("catalog name", null, null, null);
if (views.next()) {
System.out.println("View exists");
} else {
System.out.println("View does not exist");
}
String query = "SELECT * FROM dotcom";
String view = "CREATE VIEW view as SELECT FROM dotcom";
ResultSet rs = executeSelect(connection, query);
try {
while (rs.next()) {
//System.out.print("Column 1 returned ");
//System.out.println(rs.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
dropTable(connection, "dotcom");
createTable(connection, "dotcom (rank int primary key, name varchar(128), type varchar(128), subtype varchar(128), subsubtype varchar(128));");
int rows = insertIntoTableFromFile(connection, "dotcom", "src/TopURLs");
System.out.println(rows + " rows inserted.");
//Print data from result set
print();
while(rs.next()) {
rs.getInt("rank");
String name = rs.getString("name");
System.out.println(name);
}
rs.close();
}
}

PostgreSQL: How do you insert rows of data with a loop into a database table?

How can you read & insert rows from a text file containing data in [x, y, z] form for example:
1 google com (separated by tabs)
into a database table (without having to insert every single line manually)?
I'm new at programming!
Thank you for your time and help kind sirs and madams!
import java.sql.*;
import java.util.Scanner;
import java.io.*;
public class Database {
public static Connection connectToDatabase(String user, String port, String database) {
System.out.println("-------- PostgreSQL " + "JDBC Connection Testing ------------");
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your PostgreSQL JDBC Driver? " + "Include in your library path!");
e.printStackTrace();
}
System.out.println("PostgreSQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:postgresql://localhost:" + port + "/" + database, user,
"doesn't matter!");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
}
return connection;
}
public static ResultSet executeSelect(Connection connection, String query) {
Statement st = null;
try {
st = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
ResultSet rs = null;
try {
rs = st.executeQuery(query);
st.close();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return rs;
}
public static void dropTable(Connection connection, String table) {
Statement st = null;
try {
st = connection.createStatement();
st.execute("DROP TABLE " + table);
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void createTable(Connection connection, String tableDescription) {
Statement st = null;
try {
st = connection.createStatement();
st.execute("CREATE TABLE " + tableDescription);
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static int insertIntoTableFromFile(Connection connection, String table, String file) {
BufferedReader br = null;
int numRows = 0;
try {
Statement st = connection.createStatement();
String sCurrentLine, brokenLine[], composedLine = "";
br = new BufferedReader(new FileReader(file));
while ((sCurrentLine = br.readLine()) != null) {
// Insert each line to the DB
brokenLine = sCurrentLine.split("\t");
composedLine = "INSERT INTO dotcom VALUES (";
int i;
for (i = 0; i < brokenLine.length - 1; i++) {
composedLine += "'" + brokenLine[i] + "',";
}
composedLine += "'" + brokenLine[i] + "')";
numRows = st.executeUpdate(composedLine);
}
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return numRows;
}
public static void main(String[] argv) throws SQLException, FileNotFoundException {
#SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.println("Please enter your Username:");
String user = input.next();
System.out.println("Please enter your Port ID:");
String port = input.next();
String database = "test";
Connection connection = connectToDatabase(user, port, database);
Statement st = connection.createStatement();
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
return;
}
// Now we're ready to work on the DB
// read TopURLs file
try {
BufferedReader fileReader = new BufferedReader(new FileReader("TopURLs"));
while (fileReader.readLine() != null) {
st.execute("DROP TABLE IF EXISTS dotcom;");
st.execute("CREATE TABLE dotcom (rank integer PRIMARY KEY, domainName varchar(128), domainType varchar(128));");
st.execute("INSERT INTO dotcom VALUES(1, 'google', 'com');");
//st.execute("INSERT INTO dotcom VALUES(2, 'facebook', 'com');");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
}
/*
try {
BufferedReader fileReader = new BufferedReader(new FileReader("TopURLs"));
while (fileReader.readLine() != null) {
st.execute("DROP TABLE IF EXISTS dotcom;");
st.execute("CREATE TABLE dotcom (rank integer PRIMARY KEY, domainName varchar(128), domainType varchar(128));");
st.execute("INSERT INTO dotcom SELECT com, domainType FROM dotcom WHERE domainType = 'com'");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
}
*/
/*
Statement createStatement = null;
PreparedStatement insertStatement = null;
try {
BufferedReader fileReader = new BufferedReader(new FileReader("TopURLs"));
String line = null;
createStatement = connection.createStatement();
createStatement.executeUpdate("DROP TABLE IF EXISTS dotcom");
createStatement.executeUpdate("CREATE TABLE dotcom (rank integer PRIMARY KEY, domainName varchar(128), domainType varchar(128))");
connection.setAutoCommit(false);//commit whole batch at the end
insertStatement = connection.prepareStatement("INSERT INTO dotcom VALUES (?, ?, ?)");
while ( (line = fileReader.readLine()) != null) {
line = fileReader.readLine();
String[] urls = line.split("\t");//space or any other delimiter that you're using
insertStatement.setInt(1, Integer.parseInt(urls[0]));
insertStatement.setString(2, urls[1]);
insertStatement.setString(3, urls[2]);
//insertStatement.setString(4, urls[3]);
insertStatement.addBatch();
}
insertStatement.executeBatch();
connection.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(connection != null) {
connection.setAutoCommit(true);
}
if(createStatement != null) {
createStatement.close();
}
if(insertStatement != null) {
insertStatement.close();
}
}
*/
// connection is of type Connection (in JDBC)
DatabaseMetaData dbm = connection.getMetaData();
// check if table is there
ResultSet tables = dbm.getTables(null, null, "table name", null);
if (tables.next()) {
System.out.println("Table exists");
} else {
System.out.println("Table does not exist");
}
// check if view is there?
//"create view foo as select * from table;"
//"select * from foo;"
ResultSet views = dbm.getTables("catalog name", null, null, null);
if (views.next()) {
System.out.println("View exists");
} else {
System.out.println("View does not exist");
}
String query = "SELECT * FROM internet";
ResultSet rs = executeSelect(connection, query);
try {
while (rs.next()) {
System.out.print("Column 1 returned ");
System.out.println(rs.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
rs.close();
dropTable(connection, "dotcom");
createTable(connection,
"dotcom (id int primary key, name varchar(15), type varchar(15));");
int rows = insertIntoTableFromFile(connection, "dotcom", "TopURLs");
System.out.println(rows + " rows inserted.");
}
}
This is too long for a comment.
When loading data from a text file, I generally load into a staging table that consists of character column. Then, I can do validate the data, convert to the appropriate data types, and even reparse some columns, if necessary.
Admittedly, the Postgres COPY command often works quite well just reading in the data.
You would seem to have two options:
Load the data into a staging table and do the data cleansing there.
Read the data line-by-line in java and do the data cleansing there.
Because my SQL skills >> my java skills, it is a no-brainer which I would choose (SQL). But these would seem to be your most reasonable options.

Selecting a count from my database using Java and Netbeans

I have the following code:
try {
String sql = "SELECT COUNT(*) AS \"Jumlah\" FROM dokter";
ResultSet rs = connection.st.executeQuery(sql);
if(rs.next()){
abc = rs.getString("Jumlah").toString();
}
} catch (Exception e) {
System.out.println("\n Message: " + e.getMessage());
}
Why can't my ResultSet execute the given SQL?
Lose the alias, it's just an unnecessary complication. Just reference the ResultSet by the column's index:
try {
String sql = "SELECT COUNT(*) FROM dokter";
ResultSet rs = connection.st.executeQuery(sql);
if(rs.next()) {
abc = rs.getInt(1); // or getString(1) if you need it as a String
}
} catch (Exception e) {
System.out.println("\n Message: " + e.getMessage());
}
I suggest you use a PreparedStatement and a try-with-resources to close it (and your ResultSet). A count is not a String, and if you have a Connection connection then you might do something like
int count = 0;
try {
String sql = "SELECT COUNT(*) FROM dokter";
try (PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
count = rs.getInt(1);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

org.apache.commons.dbcp.DelegatingPreparedStatement is closed

org.apache.commons.dbcp.DelegatingPreparedStatement
is closed
Could i know in which situations this exception will come.
I closed all result sets and prepared statements.
How can i solve this problem.
Code :
public int UpdateMovementLines(List<MaterialRequestIssuanceVO> mlinelist,String projId,String documentno,String user){
int count = 1;
int line = 0;
String uom = null;
String projLocatorId = null;
String projWarehouseId = null;
String warehouseLocatorId = null;
String issuanceId = null;
String movementLineId =null;
String pinstanceId = null;
String sqlQry = null;
String whLocatorId = null;
PreparedStatement ps = null;
PreparedStatement ps1 = null;
PreparedStatement ps2 = null;
PreparedStatement ps3 = null;
PreparedStatement ps4 = null;
PreparedStatement ps5 = null;
ResultSet rs = null;
ResultSet rs1 = null;
ResultSet rs2 = null;
ResultSet rs3 = null;
try{
conn.setAutoCommit(false);
try{
sqlQry="INSERT INTO m_movement (m_movement_id, AD_CLIENT_ID, AD_ORG_ID, CREATED, CREATEDBY, UPDATED, UPDATEDBY," +
"name, movementdate, posted, processing, move_fromto_locator,documentno) VALUES " +
"(?,?,?,NOW(),?,NOW(),?,to_char(now(),'DD-MM-YYYY'),now(),?,?,?,?)";
ps = conn.prepareStatement(sqlQry);
for(MaterialRequestIssuanceVO movementvo:mlinelist){
issuanceId = movementvo.getIssuanceid();
ps.setString(1, issuanceId);
ps.setString(2,movementvo.getClientid());
ps.setString(3,movementvo.getOrgid());
ps.setString(4, movementvo.getCreatedby());
ps.setString(5,movementvo.getUpdatedby());
ps.setString(6,"N");
ps.setString(7,"N");
ps.setString(8,"N");
ps.setString(9, documentno);
count=ps.executeUpdate();
}
}
catch (SQLException e) {
// TODO Auto-generated catch block
log4j.info("Inside DB Line saveMRIssuanceMovementData Exception"+e);
}
finally
{
try
{
ps.close();
log4j.info("Inside Line Finally");
} catch (SQLException e) {
e.printStackTrace();
}
}
ps=conn.prepareStatement("select c_uom_id as uom from m_product where m_product_id = ?");
for(MaterialRequestIssuanceVO movementvo:mlinelist){
line = line +10;
ps.setString(1, movementvo.getMaterialid());
rs = ps.executeQuery();
while(rs.next())
{
uom = rs.getString("uom");
log4j.info("Uom: "+uom);
}
try{
ps2=conn.prepareStatement("select m_locator_id as locatorid from m_locator where m_warehouse_id = ?");
ps2.setString(1, movementvo.getWarehouseId());
rs2 = ps2.executeQuery();
while(rs2.next())
{
warehouseLocatorId = rs2.getString("locatorid");
log4j.info("warehouseLocatorId: "+warehouseLocatorId);
}
}catch(SQLException e){
log4j.info("Warehouse Locator Exception: "+e);
}
finally{
rs2.close();
ps2.close();
}
try{
ps3=conn.prepareStatement("select m_locator_id as locatorid from m_locator where m_warehouse_id=? and value like ?");
ps3.setString(1, movementvo.getWarehouseId());
ps3.setString(2, projId);
rs3 = ps3.executeQuery();
if(rs3.next())
{
projLocatorId = rs3.getString("locatorid");
log4j.info("projLocatorId: "+projLocatorId);
}
else
{
sqlQry="INSERT INTO m_locator (m_locator_id, AD_CLIENT_ID, AD_ORG_ID, CREATED, CREATEDBY, UPDATED, UPDATEDBY," +
"value, m_warehouse_id, priorityno, x,y, z) VALUES " +
"(?,?,?,NOW(),?,NOW(),?,?,?,?,?,?,?)";
ps4 = conn.prepareStatement(sqlQry);
try
{
whLocatorId = SequenceIdData.getUUID();
log4j.info("issueid: "+whLocatorId);
ps4.setString(1, whLocatorId);
log4j.info("Client Id: "+movementvo.getClientid());
ps4.setString(2,movementvo.getClientid());
log4j.info("Orgid: "+movementvo.getOrgid());
ps4.setString(3,movementvo.getOrgid());
ps4.setString(4, movementvo.getCreatedby());
ps4.setString(5,movementvo.getUpdatedby());
ps4.setString(6,projId);
ps4.setString(7,movementvo.getWarehouseId());
ps4.setInt(8,50);
ps4.setString(9,"x");
ps4.setString(10,"y");
ps4.setString(11,"z");
count=ps4.executeUpdate();
if(count == 1)
projLocatorId = whLocatorId;
}
catch(SQLException e)
{
log4j.info("M_Locator Exception: "+e);
}
finally
{
ps4.close();
}
log4j.info("whLocatorId projLocatorId: "+projLocatorId);
}
}catch(SQLException e){
log4j.info("Locator Exception: "+e);
}
finally{
rs3.close();
ps3.close();
}
try{
sqlQry="INSERT INTO m_movementline (m_movementline_id, AD_CLIENT_ID, AD_ORG_ID, CREATED, CREATEDBY, UPDATED, UPDATEDBY," +
"m_movement_id, m_locator_id, m_locatorto_id, m_product_id,line, movementqty,c_uom_id,m_attributesetinstance_id) VALUES " +
"(?,?,?,NOW(),?,NOW(),?,?,?,?,?,?,?,?,?)";
ps1 = conn.prepareStatement(sqlQry);
movementLineId = SequenceIdData.getUUID();
ps1.setString(1, movementLineId);
ps1.setString(2,movementvo.getClientid());
ps1.setString(3,movementvo.getOrgid());
ps1.setString(4, movementvo.getCreatedby());
ps1.setString(5,movementvo.getUpdatedby());
ps1.setString(6,issuanceId);
ps1.setString(7,warehouseLocatorId);
ps1.setString(8,projLocatorId);
ps1.setString(9,movementvo.getMaterialid());
ps1.setInt(10,line);
ps1.setInt(11,Integer.parseInt(movementvo.getIssuedqty()));
ps1.setString(12,uom);
ps1.setString(13,"0");
count=ps1.executeUpdate();
}
catch(SQLException e){
log4j.info("Inside DB MoveLines SQLException"+e.getMessage());
}
finally
{
try
{
ps1.close();
log4j.info("Inside movement Line Finally");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
sqlQry="INSERT INTO ad_pinstance (ad_pinstance_id, AD_CLIENT_ID, AD_ORG_ID, CREATED, CREATEDBY, UPDATED, UPDATEDBY," +
"ad_process_id, record_id, isprocessing, ad_user_id,result) VALUES " +
"(?,?,?,NOW(),?,NOW(),?,?,?,?,?,?)";
ps5 = conn.prepareStatement(sqlQry);
for(MaterialRequestIssuanceVO movementvo:mlinelist){
try{
pinstanceId = SequenceIdData.getUUID();
log4j.info("pinstanceId: "+pinstanceId);
ps5.setString(1, pinstanceId);
log4j.info("Client Id: "+movementvo.getClientid());
ps5.setString(2,movementvo.getClientid());
log4j.info("Orgid: "+movementvo.getOrgid());
ps5.setString(3,movementvo.getOrgid());
ps5.setString(4, movementvo.getCreatedby());
ps5.setString(5,movementvo.getUpdatedby());
ps5.setString(6,"122");
ps5.setString(7,issuanceId);
ps5.setString(8,"N");
ps5.setString(9,user);
ps5.setInt(10, Integer.parseInt("1"));
count=ps5.executeUpdate();
}
catch(SQLException e){
log4j.info("saveMRIssuanceMovementData Line SQLException"+e.getMessage());
}
finally
{
try
{
ps5.close();
log4j.info("Inside movement Line Finally");
} catch (SQLException e) {
e.printStackTrace();
}
}
try{
ps=conn.prepareStatement("select m_movement_post(?)");
ps.setString(1, pinstanceId);
rs = ps.executeQuery();
while(rs.next()){
log4j.info("Result Set: "+rs.getString(1));
}
}catch(SQLException e){
log4j.info("Movement Post Exception: "+e);
}
finally{
ps.close();
}
}
conn.commit();
} catch (Exception e) {
try {
conn.rollback();
count = 0;
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//Above Mensined Exception Catching hear
log4j.info("Inside DB saveMRIssuanceMovementData Line SQLException"+e.getMessage());
}
finally
{
try
{
if(conn != null)
conn.close();
}
catch(SQLException e)
{
}
}
return count;
}
PreparedStatement is closed
You can get this exception when you're trying to (re)use a PreparedStatement while it has been closed. Check the line number of the first line in the stacktrace. It should hint which PreparedStatement it is talking about. Then backtrack its use in the code and fix code accordingly.
Judging the flood of code you've posted, I suspect that it's the ps5 which is been created before a for loop and been closed inside the for loop. Here's an extract of relevance from your code:
ps5 = conn.prepareStatement(sqlQry);
for (MaterialRequestIssuanceVO movementvo : mlinelist) {
try {
ps5.setString(1, string);
ps5.executeUpdate();
} finally {
ps5.close(); // You're closing inside the loop!
}
}
The next iteration in the loop won't be able to reuse the same PreparedStatement anymore. The fix is obvious: close it after completion of the for loop.
try {
ps5 = conn.prepareStatement(sqlQry);
for (MaterialRequestIssuanceVO movementvo : mlinelist) {
ps5.setString(1, string);
ps5.executeUpdate();
}
} finally {
ps5.close();
}
That said, logging all exceptions as Info or doing only e.printStackTrace() and suppressing them and continuing the code flow isn't always a good idea. Log them as Error and then hard-throw thereafter.
} catch (Exception e) {
logger.error("Your message", e);
throw e;
}
Rethrowing isn't needed for exceptions during close, but logging them as Warn is useful.
Last but not least, consider refactoring the exceptionally large method block into separate and sensible methods (tasks) ;)

Categories