java.sql.SQLException: no such column: after accepting sql query - java

I am developing a system that manages stock for a project and am trying to get a list of stock items that are close to their expiration date
This is what the table is created as:
Query_Statement.executeUpdate("CREATE TABLE STOCK_PURCHASE ( SP_ID INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "USER_ID INTEGER,STOCK_ID INTEGER,SUPPLIER_ID INTEGER,SP_ENTRY_DATE TEXT,"
+ "SP_PURCHASE_PRICE REAL, SP_SELLBY_DATE TEXT, SP_QUANTITY REAL,"
+ "FOREIGN KEY (USER_ID) REFERENCES USER (USER_ID),"
+ "FOREIGN KEY (STOCK_ID) REFERENCES STOCK (STOCK_ID),"
+ "FOREIGN KEY (SUPPLIER_ID) REFERENCES SUPPLIER (SUPPLIER_ID))");
This is the function to get that I use:
public ArrayList<String> GetExpiredStock(){
Connection dbConnection = null;
ResultSet list = null;
ArrayList<String> ls = new ArrayList<String>();
LocalDate currentDate = LocalDate.now();
//System.out.println(today);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
try {
dbConnection = DriverManager.getConnection("jdbc:sqlite:"+dbName+".db");
Statement Query_Statement = dbConnection.createStatement();
Query_Statement.setQueryTimeout(10);
list = Query_Statement.executeQuery("SELECT SP_ENTRY_DATE, STOCK_ID FROM STOCK_PURCHASE"); //this works
while (list.next()) {
try {
LocalDate expDate = LocalDate.parse(list.getString("SP_SELLBY_DATE"), formatter);
LocalDate monthAway = expDate.minusMonths(1);
System.out.println(currentDate);
if(currentDate.isAfter(monthAway)) {
int id = list.getInt("STOCK_ID");
ResultSet ids = Query_Statement.executeQuery("SELECT STOCK_NAME FROM STOCK WHERE STOCK_ID=" + id);
ls.add(ids.getString("STOCK_NAME") + "\t\t" +
list.getString("SP_SELLBY_DATE") + getStockQuant(list.getInt("STOCK_ID"),
currentDate));
}
}catch(SQLException e) {
System.err.println(e);
continue;
}
}
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
try {
if (dbConnection != null)
dbConnection.close();
} catch (SQLException e) {
System.err.println(e);
}
}
return ls;
}
I expect it to get the expiration date. However it keeps saying:
java.sql.SQLException: no such column: 'SP_SELLBY_DATE'
Edit:
I changed the code to look like this:
public ArrayList<String> GetExpiredStock(){
Connection dbConnection = null;
ResultSet list = null;
ArrayList<String> ls = new ArrayList<String>();
LocalDate currentDate = LocalDate.now();
//System.out.println(today);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
try {
dbConnection = DriverManager.getConnection("jdbc:sqlite:"+dbName+".db");
Statement Query_Statement = dbConnection.createStatement();
Query_Statement.setQueryTimeout(10);
list = Query_Statement.executeQuery("SELECT SP_SELLBY_DATE, STOCK_ID FROM STOCK_PURCHASE"); //this works
while (list.next()) {
try {
String da = list.getString("SP_SELLBY_DATE");
int id = list.getInt("STOCK_ID");
System.out.println("Executed on id = " + id);
LocalDate expDate = LocalDate.parse(da, formatter);
LocalDate monthAway = expDate.minusMonths(1);
System.out.println(currentDate);
if(currentDate.isAfter(monthAway)) {
ResultSet ids = Query_Statement.executeQuery("SELECT STOCK_NAME FROM STOCK WHERE STOCK_ID=" + id);
ls.add(ids.getString("STOCK_NAME") + "\t\t" +
da + "\t\t"+ getStockQuant(id,
currentDate));
}
}catch(SQLException e) {
System.err.println(e);
continue;
}
}
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
try {
if (dbConnection != null)
dbConnection.close();
} catch (SQLException e) {
System.err.println(e);
}
}
return ls;
But it still fails after the first iteration

Your SQL query doesn't have the required column, add it :
SELECT SP_ENTRY_DATE, STOCK_ID, SP_SELLBY_DATE FROM STOCK_PURCHASE

I believe you query it list.getString("SP_SELLBY_DATE") twice in your loop, that should be root cause. Instead, you should use a variable when you get it to avoid to call it again as cursor has changed.

Related

SQL executeUpdate() seems to commit data, but that is not the case. How can I find my error?

I curently work on few SQL queries (MSSQL 2O14), but only "SELECT" query works with executeQuery().
I had use execute() and executeUpdate() on "INSERT INTO" and "UPDATE" queries, but whereas it looks like working, no way.
FYI, in "UPDATE_PREVIOUS_H_LOT_STATUT,
int count= p.executeUpdate(); return 1. If h_lot_number is an unknown lot number, count = 0.
So, if I use wrong data in input, my query isn't executed(Until here, I agree) but when I use the expected data, the query is executed but there is no change in my DB.
How can I find where my error is ?
UPDATE Function :
public static boolean UPDATE_PREVIOUS_H_LOT_STATUT(String h_lot_number_old) {
try {
setUpConnexion("mainDB");
String baseQuery = "UPDATE valve_assembly_h SET statut = 'Expiré' WHERE h_lot_number = '" + h_lot_number_old + "'";
//PreparedStatement p = newTransact("UPDATE valve_assembly_h SET statut = 'Expiré' WHERE h_lot_number = '" + h_lot_number_old + "'", "mainDB");
PreparedStatement toReturn = (PreparedStatement) mainCon.prepareStatement(baseQuery);
int count = toReturn.executeUpdate();
if (count > 0) {
Log.d("Sucess : ", "Previous h_lot updated.");
closeCons();
return true;
} else {
Log.e("Error : ", "No lot found with this number.");
closeCons();
return false;
}
} catch (SQLException e) {
error = e.getMessage();
Log.e("Error :", error);
closeCons();
return false;
}
}
LOAD PREVIOUS NUMBER FUNCTION (works perfectly)
public static String LOAD_PREVIOUS_H_LOT_NUMBER(String machineNumber) {
String s = "";
try {
setUpConnexion("mainDB");
ResultSet RS = executeQuery("SELECT h_lot_number FROM valve_assembly_h WHERE machine_number = '" + machineNumber + "' AND statut = 'Actif'", "mainDB");
while (RS.next()) {
s = RS.getString(1);
Log.d("Success : ", "Lot number : " + s);
}
closeResultSet(RS);
} catch (Exception e) {
error = e.getMessage();
Log.e("Error :", error);
s = error;
}
closeCons();
return s;
}
Set up connection function : (works perfectly)
public static boolean setUpConnexion(String DBNAME) {
StrictMode.ThreadPolicy policy;
policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String connectURL;
try {
CONNECTION MS SQL SERVER
}
return true;
} catch (Exception e) {
System.out.println("SQL ERROR: " + e.getMessage());
e.printStackTrace();
return false;
}
}
Try to commit the transaction manually through the Connection object ,hope it helps.

Android :java.sql.SQLException: No current row in the ResultSet

I am getting the following error when i try to execute my query(s),but i don't know why it gives me the following error every time i try to execute my query(s).The error also appears in my logcat and not in a toast as i expected.
here is my code!!
#Override
protected String doInBackground(String... strings) // Connect to the database, write query and add items to array list
{
runOnUiThread(new Runnable() {
public void run() {
try {
Connection conn = connectionClass.CONN(); //Connection Object
if (conn == null) {
success = false;
msg = "Sorry something went wrong,Please check your internet connection";
} else {
// Change below query according to your own database.
Date c = Calendar.getInstance().getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyy/mm/dd");
String formattedDate = df.format(c);
System.out.println("it isssssssssssssssssssssssssssssssssssssssssaaaaaaaaaaaaaaaaaaaa"+getIntent().getStringExtra("nameid"));
String query = "Insert into CustomerSupportChat values('" + formattedDate + "','" + themsg.getText().toString() + "','Customer','3','"+getIntent().getStringExtra("nameid")+"','1','1') " +
"Select MessageID,MessageDate,MessageText,SenderType,MessageRecieved,MessageReaded,Users_Login_Data.Username,StoresData.StoreEnglishName,StoresData.StoreArabicName FROM " +
"CustomerSupportChat INNER JOIN Users_Login_Data ON " +
"CustomerSupportChat.CustomerID = Users_Login_Data.CustomerID INNER JOIN StoresData ON " +
"CustomerSupportChat.StoreID = StoresData.StoreID";
String query2 =
"Select MessageID,MessageDate,MessageText,SenderType,MessageRecieved,MessageReaded,Users_Login_Data.Username,StoresData.StoreEnglishName,StoresData.StoreArabicName FROM " +
"CustomerSupportChat INNER JOIN Users_Login_Data ON " +
"CustomerSupportChat.CustomerID = Users_Login_Data.CustomerID INNER JOIN StoresData ON " +
"CustomerSupportChat.StoreID = StoresData.StoreID Where SenderType = 'Store'";
Statement stmt = conn.createStatement();
Statement stmt2 = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSet rs2 = stmt2.executeQuery(query2);
if (rs != null) // if resultset not null, I add items to itemArraylist using class created
{
while (rs.next()) {
try {
itemArrayList.add(new ClassListChat(rs.getString("MessageDate"), rs.getString("MessageText"), rs.getString("SenderType"), rs2.getString("MessageText")));
themsg.setText("");
} catch (Exception ex) {
ex.printStackTrace();
}
}
msg = "Found";
success = true;
} else {
msg = "No Data found!";
success = false;
}
}
} catch (Exception e) {
e.printStackTrace();
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
msg = writer.toString();
Log.d("Error", writer.toString());
success = false;
}
}
});
return msg;
}
I have tried removing while and replacing it with if statement but it showed me the same error.
I also tried my query on mssql and it executed successfully.
EDIT I solved it by changing the date format to MM/dd/yyyy.
But now i get the following error:
java.sql.SQLException: No current row in the ResultSet.
Any ideas?
Looks like there is no null check for rs2 in the below line so could throw that error:
itemArrayList.add(new ClassListChat(rs.getString("MessageDate"), rs.getString("MessageText"), rs.getString("SenderType"), rs2.getString("MessageText")));
Also a better way to check for ResultSet not returning anything is like this:
if (rs.next() == false){
msg = "No Data found!";
success = false;
} else {
do {
// add items to itemArraylist...
} while (rs.next());
}
Read more: https://javarevisited.blogspot.com/2016/10/how-to-check-if-resultset-is-empty-in-Java-JDBC.html#ixzz6KIFZZHNB

How to tell the mysql table to retrieve the next matching record into my default table model

How do I tell my java program to retrieve the next matching record into my default table model.
Below is my home work so far.using jTable tb1 and default table model dtm is compulsory for me.
private void Show_My_LettersActionPerformed(java.awt.event.ActionEvent evt) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(url, "root", "root");
System.out.println("Connected database successfully...");
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql = "SELECT * from LCI where SUB_ID = '" + SUB_ID_.getText() + "' AND L_DATE = '" + DATE.getText() + "'";
ResultSet rs1, rs2, rs3, rs4, rs5, rs6, rs7;
rs1=j.getData("select COUNT(*) from LCI");
try (ResultSet rs = stmt.executeQuery(sql)) {
DefaultTableModel dtm = (DefaultTableModel) tb1.getModel();
while (rs.next()) {
String L_ID_ = rs.getString("L_ID");
String L_DATE_ = rs.getString("L_DATE");
String heading = rs.getString("HEADING");
String sub_id = rs.getString("SUB_ID");
System.out.print("ID: " + L_ID_);
System.out.print(", Letter date: " + L_DATE_);
System.out.print(", Heading " + heading);
System.out.println(", Subject ID " + sub_id);
/* This gives the correct out put when debug is done.
But the below code doesn't retrive the full out put.
It gives only the very first record matching with the user inputs*/
Vector v = new Vector();
Vector v1 = new Vector();
Vector v2 = new Vector();
Vector v3 = new Vector();
JOptionPane.showMessageDialog(this, "Done");
dtm.getColumnName(1);
v.addElement(rs.getString(1));
dtm.addColumn(v);
dtm.getColumnName(3);
v1.addElement(rs.getString(3));
dtm.addColumn(v1);
dtm.getColumnName(10);
v2.addElement(rs.getString(10));
dtm.addColumn(v2);
// stmt.executeQuery("SELECT * FROM LCI WHERE L_ID IN(SELECT (L_ID + 1)FROM LCI WHERE L_DATE = '"+DATE.getText()+"'");
dtm.addRow(v3);
//stmt.executeQuery("SELECT * FROM LCI WHERE L_ID IN(SELECT (L_ID '"+(+1)+"')FROM LCI WHERE L_DATE = '"+DATE.getText()+"'");
}
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}
}// TODO add your handling code here:
}

ResultSet returns only the last record from the table

The below java method sets the ResultSet data to a bean class and I am fetching the data. But, the method runHiveQuery() returns only one row that is the last record in the table. While debugging the code i found that the resultset is being looped twice as we have two records. But, while returning the bean class object there is some issue as it retrieves only one record.
Unable to find what is going wrong.
public CSPData getCSPData() throws SQLException {
try {
String drivername = "org.apache.hive.jdbc.HiveDriver";
Class.forName(drivername);
connection = DriverManager.getConnection("jdbc:hive2://hddev-c01-edge-01:20000/");
statement = connection.createStatement();
resultset = statement.executeQuery(
"select distinct db_name as db_name,db_server_name as db_server_name,lower(db_name) as l_db_name,lower(db_server_name) as l_server_name,regexp_replace(lower(db_server_name), '-', '_') as server_name,db_server_name_secondary as db_server_name_secondary from csp.curated_input");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
while (resultset.next()) {
cspdata.setDbName(resultset.getString("db_name"));
cspdata.setDbServerName(resultset.getString("db_server_name"));
cspdata.setDbServerNameSecondary(resultset.getString("db_server_name_secondary"));
cspdata.setlDbName(resultset.getString("l_db_name"));
cspdata.setlServerName(resultset.getString("l_server_name"));
cspdata.setServerName(resultset.getString("server_name"));
}
return cspdata;
}
public void runHiveQuery() throws SQLException {
CSPData cspdata = hivedao.getCSPData();
String hive_db = "csp";
String dbname = cspdata.getDbName();
String dbservername = cspdata.getDbServerName();
String servername = cspdata.getlServerName();
String drop = "Drop table if exists " + hive_db + "." + "IB_C3_" + dbname + "_" + dbservername;
String insert = "insert into table " + hive_db + "." + "IB_export_log select " + "\'ib_c3_" + dbname + "_"
+ servername + "\' from " + hive_db + "." + "dual limit 1";
System.out.println(drop);
System.out.println(insert);
}
Your code returns the last record since it only returns a single record. You should return a List :
public List<CSPData> getCSPData() throws SQLException {
List<CSPData> result = new ArrayList<>();
try {
String drivername = "org.apache.hive.jdbc.HiveDriver";
Class.forName(drivername);
connection = DriverManager.getConnection("jdbc:hive2://hddev-c01-edge-01:20000/");
statement = connection.createStatement();
resultset = statement.executeQuery(
"select distinct db_name as db_name,db_server_name as db_server_name,lower(db_name) as l_db_name,lower(db_server_name) as l_server_name,regexp_replace(lower(db_server_name), '-', '_') as server_name,db_server_name_secondary as db_server_name_secondary from csp.curated_input");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
while (resultset.next()) {
CSPData cspdata = new CSPData ();
cspdata.setDbName(resultset.getString("db_name"));
cspdata.setDbServerName(resultset.getString("db_server_name"));
cspdata.setDbServerNameSecondary(resultset.getString("db_server_name_secondary"));
cspdata.setlDbName(resultset.getString("l_db_name"));
cspdata.setlServerName(resultset.getString("l_server_name"));
cspdata.setServerName(resultset.getString("server_name"));
result.add(cspdata);
}
return result;
}

Exhausted Resultset SQL Exception occur in if condition else condition

I written following code but it throws a "Resultset exhausted" error.
String dt = rs.getTimestamp("GuaranteeDate")+"";
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat1.parse(dt);
DateTime dateTime = new DateTime(date);
Integer attributeID = 0;
String query1 ="select distinct M_attributesetinstance_id from M_storage where m_Product_id="+M_Product_ID;
attributeID = DB.getSQLValue(trxName, query1);
Timestamp MaufacuringDate = null;
String query = "select manufacturingdate from m_attributesetinstance where m_attributesetinstance_id="+attributeID;
try
{
pstmt = null;
rs = null;
pstmt = DB.prepareStatement(query.toString(),null);
rs = pstmt.executeQuery();
while (rs.next())
{
MaufacuringDate = rs.getTimestamp("manufacturingdate");
}
}
catch (Exception e)
{
e.printStackTrace();
}
if (MaufacuringDate!= null)
{
DateTime ManufacturingDate = new DateTime(MaufacuringDate);
try
{
if((!"".equalsIgnoreCase(dt) || dt!=null) && percentage>=0 && GuaranteeDate != null)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c1 = Calendar.getInstance();
Date d1 = c1.getTime();
DateTime dateTime1 = new DateTime(d1);
try
{
// c1.setTime(sdf.parse(dt));
// ReadableInstant date2;
Days d = Days.daysBetween(ManufacturingDate, dateTime);
int days = d.getDays();
float calulateddays = (float)(days*(percentage/100.0f));
Integer roundeddays = Math.round(calulateddays);
c1.setTime(sdf.parse(dt));
c1.add(Calendar.DATE, -roundeddays); // number of days to add
try
{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date parsedDate = dateFormat.parse(sdf.format(c1.getTime()));
MinGuaranteeDate = new java.sql.Timestamp(parsedDate.getTime());
}
catch (Exception e)
{
MinGuaranteeDate = null;
e.printStackTrace();
}
}
catch (ParseException e)
{
MinGuaranteeDate = null;
e.printStackTrace();
}
}
else
{
continue;
}
}
catch(Exception e)
{
MinGuaranteeDate = null;
e.printStackTrace();
}
System.out.println("MinGuaranteeDate :"+MinGuaranteeDate);
System.out.println("TodayDate :"+GuaranteeDate);
if(MinGuaranteeDate==null || MinGuaranteeDate.after(GuaranteeDate))
{
continue;
}
}
else
{
Timestamp GRNDate = null;
String query2 = "select distinct Movementdate from M_inout m "
+"inner join m_inoutline mil ON (m.M_Inout_ID = mil.M_Inout_id) where mil.M_Product_Id="+M_Product_ID;
try
{
pstmt = null;
rs = null;
pstmt = DB.prepareStatement(query2.toString(),null);
rs = pstmt.executeQuery();
while (rs.next())
{
GRNDate = rs.getTimestamp("Movementdate");
}
}
catch (Exception e)
{
e.printStackTrace();
}
// String dt1 = rs.getTimestamp("GuaranteeDate")+"";
SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = dateFormat1.parse(dt);
DateTime dateTime2 = new DateTime(date1);
DateTime GRDate = new DateTime(GRNDate);
try{
if((!"".equalsIgnoreCase(dt) || dt!=null) && percentage>=0 && GuaranteeDate!=null) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c1 = Calendar.getInstance();
Date d1 = c1.getTime();
//DateTime dateTime1 = new DateTime(d1);
try {
// c1.setTime(sdf.parse(dt));
//ReadableInstant date2;
Days d = Days.daysBetween(GRDate, dateTime);
int days = d.getDays();
float calulateddays = (float)(days*(percentage/100.0f));
Integer roundeddays = Math.round(calulateddays);
c1.setTime(sdf.parse(dt));
c1.add(Calendar.DATE, -roundeddays); // number of days to add
try{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date parsedDate = dateFormat.parse(sdf.format(c1.getTime()));
MinGuaranteeDate = new java.sql.Timestamp(parsedDate.getTime());
}catch(Exception e){
MinGuaranteeDate = null;
e.printStackTrace();
}
} catch (ParseException e) {
MinGuaranteeDate = null;
e.printStackTrace();
}
}
else {
continue;
}
}catch(Exception e) {
MinGuaranteeDate = null;
e.printStackTrace();
}
System.out.println("MinGuaranteeDate :"+MinGuaranteeDate);
System.out.println("TodayDate :"+GuaranteeDate);
if(MinGuaranteeDate==null || MinGuaranteeDate.before(GuaranteeDate)) {
continue;
}
}
}
if (rs.getBigDecimal(11).signum() == 0) {
list.add(new MStorage(ctx, rs, trxName));
}
}
}
} catch (Exception e) {
s_log.log(Level.SEVERE, sql, e);
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
in above code gives the error on statement of bigdecimal
if (rs.getBigDecimal(11).signum() == 0) {
list.add(new MStorage(ctx, rs, trxName));
}
on this line
Kindly help me out
You need to find out whether MStorage calls rs.next() again, as you already used up all the results within another block:
while (rs.next())
{
MaufacuringDate = rs.getTimestamp("manufacturingdate");
}
I guess you have iterated over the record using Resultset object rs and now your rs cursor is at the end of the record/ or we can say it not pointing to any record.
Still your code below is using rs object which is exhausted and not pointing to any record/table row.
if (rs.getBigDecimal(11).signum() == 0) {
list.add(new MStorage(ctx, rs, trxName));
}
Possible solution :: Create a new statement, execute query and get the new ResultSet object whose cursor will be at the start of record (Note: Cursor at -1 (use .next())) and iterate over the record ad get the required result.
OR use some logic inside previous iteration over record and get the result there itself instead of iterating again .

Categories