DB2 Error SQLCODE=-103, SQLSTATE=42604 - java

I am trying to update a table, but it isn't working and giving this sql error.
//Updating Buy Table
Integer stkbid = Integer.parseInt(request.getParameter("stockBid"));
System.out.println("stock buy id : " + stkbid);
//get buy details
PreparedStatement stmtbuy = conn.prepareStatement(
"SELECT \"StockSymbol\", \"Unit\", \"Price\", \"ClearingFee\", \"StampDuty\", \"BrokerFee\"" +
"FROM SPM.\"StockBuy\" WHERE \"StockBuyId\" = '"+ stkbid + "'");
System.out.println("Got stock buy details");
ResultSet rs=stmtbuy.executeQuery();
rs.next();
//String stkcode = rs.getString("StockSymbol");
Integer stkunit = Integer.parseInt(rs.getString("Unit"));
stkunit -= stock.getStockUnit();
Double stkprice = Double.parseDouble(rs.getString("Price"));
Double stkclear = Double.parseDouble(rs.getString("ClearingFee"));
Double stksd = Double.parseDouble(rs.getString("StampDuty"));
Double stkbfee = Double.parseDouble(rs.getString("BrokerFee"));
Double stkval = stkunit * stkprice;
Double stknv = stkval + stkval * (stkclear + stksd + stkbfee);
System.out.println(stknv);
PreparedStatement stmtbuy1 = conn.prepareStatement(
"UPDATE SPM.\"StockBuy\" SET \"Unit\" = " + stkunit + ", \"Value\" = " + stkval + ", \"NetValue\" = " + stknv +
"WHERE \"StockBuyId\" = "+ stkbid);

You are missing a space in before the WHERE clause, which messed up your stknv.
" WHERE \"StockBuyId\" = "+ stkbid);
I think it's an obligation of any poster to remind you that you should use parametrized query. So I shall do the same.
"Please use parametrized query!"

The query that is works has a quote at the end:
" WHERE \"StockBuyId\" = '"+ stkbid + "'");
The one that fails does not
"WHERE \"StockBuyId\" = "+ stkbid);
That might have something to do with it.

Related

JDBC incorrect syntax at or near "." executeQuery

This is of course parts of a larger code. It will compile with no problem, but when I call this method I get the error
"syntax error near or at "."" at the position of stmt.executeQuery(SQL).
I would really appreciate the help!
private void Component() {
try {
Statement stmt = con.createStatement();
String SQL = "SELECT component.*, stock.amount_of_component, component.price component.component_type "
+ "FROM component JOIN stock "
+ "ON component.id = stock.component_id "
+ "ORDER BY component.component_type";
ResultSet rs = stmt.executeQuery(SQL);
rs.next();
int id = rs.getInt("ID");
int amount_of_component = rs.getInt("Amount");
String name = rs.getString("Name");
double price = rs.getDouble("Price");
String component_type = rs.getString("Type");
System.out.println(" " + id + amount_of_component + " " + name + " " + price + " " + component_type);
} catch (SQLException err)
{
System.out.println(err.getMessage());
}
}
Typo, missing a comma in the query between component.price and component.component_type :
SELECT component.*, stock.amount_of_component, component.price, component.component_type
FROM component JOIN stock
ON component.id = stock.component_id
ORDER BY component.component_type
Edit: To read the whole result set, put this cycle instead of rs.next()
while(result.next()) {
int id = rs.getInt("ID");
int amount_of_component = rs.getInt("Amount");
String name = rs.getString("Name");
double price = rs.getDouble("Price");
String component_type = rs.getString("Type");
System.out.println(" " + id + amount_of_component + " " + name + " " + price + " " + component_type);
}
Edit2: To print the header, you have to do it manually by putting a System.out.println(" id amount_of_component name price component_type "); before the while.
You missed a comma between 'component.price' and 'component.component_type'

ResultSet with variable

Ok so basically I have this code:
resultSet = statement.executeQuery("select * from FEEDBACK.COMMENTS");
writeResultSet(resultSet);
private void writeResultSet(ResultSet resultSet) throws SQLException {
System.out.println("jestem w writeresultset");
// resultSet is initialised before the first data set
while (resultSet.next()) {
// it is possible to get the columns via name
// also possible to get the columns via the column number
// which starts at 1
// e.g., resultSet.getSTring(2);
String id = resultSet.getString("id");
String user = resultSet.getString("IMIE");
String website = resultSet.getString("NAZWISKO");
String summary = resultSet.getString("ADRES");
String date = resultSet.getString("EMAIL");
String comment = resultSet.getString("TELEFON");
String opisso = resultSet.getString("OPIS");
JTextField myOutput = new JTextField(1600);
myOutput.setText("id w bazie danych to " + id + " imie to " + user
+ " nazwisko to " + website + " adres to " + summary + " email to "
+ date + " teelefon to " + comment + " opis to " + opisso);
add(myOutput);
}
}
What I want to achieve is this:
resultSet = statement.executeQuery("select * from FEEDBACK.COMMENTS
where NAZWISKO LIKE " variable );
writeResultSet(resultSet);
I want to search by variable which is already defined, however I'm stuck and have no idea how to do it like that.
Use PreparedStatement:
String nazwisko = ...
String query = "select * from FEEDBACK.COMMENTS where NAZWISKO LIKE ?";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, nazwisko);
ResultSet rs = pstmt.execute();
while (resultSet.next()) {
//...
}
In case you need to use a wildcard for your LIKE, choose one of these:
nazwisko = nazwisko + "%";
nazwisko = "%" + nazwisko;
nazwisko = "%" + nazwisko + "%";
up , there are alot weird errors with your code:
like cannot find symbol variable con or incompatible type boolean cannot be converted to resultset.
I have tried this: but there is an error when executing
preparedStatement = connect
.prepareStatement("select * from FEEDBACK.COMMENTS where NAZWISKO= ? ; ");
preparedStatement.setString(1, surname3);
while (resultSet.next()) {
String id = resultSet.getString("i
d");
String user = resultSet.getString("IMIE");
String website = resultSet.getString("NAZWISKO");
String summary = resultSet.getString("ADRES");
String date = resultSet.getString("EMAIL");
String comment = resultSet.getString("TELEFON");
String opisso = resultSet.getString("OPIS");
JTextField myOutput = new JTextField(1600);
myOutput.setText("id w bazie danych to " + id + " imie to " + user + " nazwisko to " + website + " adres to " + summary + " email to " + date + " teelefon to " + comment + " opis to " + opisso);
add(myOutput);
}
error:
the query went fine but , the error appears here "while (resultSet.next())"
SEVERE: null
java.lang.NullPointerException
at jdbcexample.Main.readDataBase(Main.java:416)
at jdbcexample.Main$7.mousePressed(Main.java:346)

Having Syntax Error in code java

Why is there a syntax error in this code?
String strSqlUpdate = "UPDATE Customers SET Contact = " + contact_num + ","
+ "Email = '" + email_add + "',"
+ "Address = '" + mail_add + "',"
+ "SurveyStatus = " + radio_group + ","
+ "Subscription = " + receive_info +
"WHERE membership_ID = '" + member_ID';
I thought my code was right.
If it is the error in your code, check all the variables that you have used are declared and initialized with proper values.
If it is the syntax of the sql that is bothering you , here is what your sql would look like if all the variables are initialized to null.
UPDATE Customers SET (Contact)null,Emailnull,Address,null,SurveyStatus,null,SubscriptionnullWHERE MembershipID =null
Use spaces in your strSqlUpdate to correct the above sql.
EDIT
What you need is something like this.
String strSqlUpdate = "UPDATE Customers SET Contact = " + contact_num
+ ",Email = '" + email_add + "'"
+ ",Address = '" + mail_add + "'"
+ ",SurveyStatus = '" + radio_group + "'"
+ ",Subscription = '" + receive_info + "' "
+ "WHERE membership_ID = '" + member_ID + "'";
I get no syntax errors when I declare and Initialize all of the variables. You have to make sure they're all initialized, within the scope of the strSqlUpdate
String contact_num = "";
String email_add = "";
String mail_add = "";
String radio_group = "";
String receive_info = "";
String member_ID = "";
String strSqlUpdate = " UPDATE Customers SET (Contact)" + contact_num + "," + "Email"
+ email_add + "," + "Address" + "," + mail_add + "," + "SurveyStatus" + "," + radio_group
+ "," + "Subscription" + receive_info + "WHERE MembershipID =" + member_ID;
Also considering you're talking about SQL syntax, adding on to what others have said, I'd advise you should use a PreparedStatement to avoid SQL injection.
PreparedStatement pst = conn.prepareStatement(
"UPDATE Customers SET (Contact) ?, ?, ?, ?, ?, ?, ? WHERE ? = ?");
pst.setString(1, contact_num);
pst.setString(2, email_add);
... and so on
An error in your current SQL syntax is this
"Subscription" + receive_info + "WHERE MembershipID
Translated as
"...Subscrptionreceive_infoWHERE MembershipID..."
You need to add spaces wherever you don't have commas

GAE query working for unit testing but not local testing

My unit testing working fine for this query, but when i run my app in local it doesn't find the column seller.
String statement = "SELECT * FROM " + TABLE_NAME + " "
+ "INNER JOIN " + DbSeller.TABLE_NAME + " seller ON video.seller = seller.id "
+ "WHERE video.name LIKE ?";
//create statement
PreparedStatement stmt = DataBase.getInstance().prepareStatement(statement);
//set data
stmt.setString(1, "%" + s + "%");
//send query
ResultSet rs = stmt.executeQuery();
//the result
while(rs.next()) {
Video v = new Video();
System.out.println("test === " + rs.getInt("seller.id")); // <---- EXCEPTION (Column not found!!!!)
set(rs, v);
listVideo.add(v);
}
stmt.close();
And if i do this instead, it is fine: (Just for the test i don't want ending up writing column by column which info i need)
String statement = "SELECT video.*, seller.id as seller_id FROM " + TABLE_NAME + " "
+ "INNER JOIN " + DbSeller.TABLE_NAME + " seller ON video.seller = seller.id "
+ "WHERE video.name LIKE ?";
//create statement
PreparedStatement stmt = DataBase.getInstance().prepareStatement(statement);
//set data
stmt.setString(1, "%" + s + "%");
//send query
ResultSet rs = stmt.executeQuery();
//the result
while(rs.next()) {
Video v = new Video();
System.out.println("test === " + rs.getInt("seller_id")); // <---- NO EXCEPTION
set(rs, v);
listVideo.add(v);
}
stmt.close();
Note: My app is running on the same offline database in MySQL, so the only difference is that i run this query through my app instead of the unit testing.
Column names seller_id (underscore) and seller.id (dot) look different to me

JAVA + Mysql PreparedStatement.setString() ArrayStoreException

I've been getting this ArrayStoreException in my code for loading records from one mysql table to another. I tried truncating the destination table and running my code again and it seems that it encounters the said exception randomly.
I was going to implement a (Spring) JdbcTemplate based DAO when i've encountered an ArrayStoreException during unit testing. I tried to recreate it with ordinary JDBC code and still encounter the error.
For my DDL:
All the columns are of type varchar, except for brthdate and birthdate which are of type Date. DEFAULT CHARSET=utf8
My code snippet:
employeesSql = "select id_no, " +
"lastname, " +
"frstname, " +
"mdlename, " +
"brthdate,sex, " +
"sss_no, " +
"tin_no " +
"from lms.pms_empf " +
"where length(trim(id_no)) > 0 " +
"and id_no <> '000000' " +
" ORDER BY id_no";
employeeSql = "select count(*) "
+ "from dim_employees "
+ "where id_no=?";
updateSql = "update dim_employees " +
"set last_name = ?, " +
"first_name = ?, " +
"middle_name = ?, " +
"birthdate = ?, " +
"sex = ?, " +
"sss_no = ?, " +
"tin_no = ? " +
"where id_no = ?";
insertSql = "insert into dim_employees " +
"(id_no, last_name, first_name, " +
"middle_name,birthdate, sex, sss_no, tin_no) " +
"values (?,?,?,?,?,?,?,?)";
employeesStmt = con.createStatement();
employeeStmt = con.prepareStatement(employeeSql);
updateStmt = con.prepareStatement(updateSql);
insertStmt = con.prepareStatement(insertSql);
employeesCursor = employeesStmt.executeQuery(employeesSql);
while (employeesCursor.next()) {
idNo = new String(employeesCursor.getBytes(1), "UTF-8");
lastName = new String(employeesCursor.getBytes(2), "UTF-8");
firstName = new String(employeesCursor.getBytes(3), "UTF-8");
middleName = new String(employeesCursor.getBytes(4), "UTF-8");
birthDate = employeesCursor.getDate(5);
sex = new String(employeesCursor.getBytes(6), "UTF-8");
sssNo = new String(employeesCursor.getBytes(7), "UTF-8");
tinNo = new String(employeesCursor.getBytes(8), "UTF-8");
employeeStmt.setString(1, idNo);
employeeCursor = employeeStmt.executeQuery();
while (employeeCursor.next()) {
if (employeeCursor.getInt(1) > 0) {
//update
updateStmt.setString(1, lastName);
updateStmt.setString(2, firstName);
updateStmt.setString(3, middleName);
updateStmt.setDate(4, birthDate);
updateStmt.setString(5, sex);
updateStmt.setString(6, sssNo);
updateStmt.setString(7, tinNo);
updateStmt.setString(8, idNo);
updateStmt.executeUpdate();
updateStmt.executeUpdate();
}
else {
//insert
insertStmt.setString(1, idNo);
insertStmt.setString(2,lastName);
insertStmt.setString(3, firstName);
insertStmt.setString(4, middleName);
insertStmt.setDate(5, birthDate);
insertStmt.setString(6, sex);
insertStmt.setString(7, sssNo);
***//exception points here** insertStmt.setString(8, tinNo);
insertStmt.executeUpdate();
insertStmt.clearParameters();
}
}
employeeStmt.clearParameters();
Stack trace:
......
Caused by: java.lang.ArrayStoreException
at java.lang.String.getChars(String.java:854)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4520)
......
at com.vdc.lmsprocs.EmployeeProc.setEmployees(EmployeeProc.java:135)
......
I'm sorry for the long post. I clearly could not explain well enough what happened here. It's the first time i've encountered such a flaw.
Thanks in advance.

Categories