I am getting the following error:
...check the syntax that corresponds to your MySQL server version for
the right syntax to use near "Item1" at line 1
Here is the relevant part of the code:
String e = e_id.getSelectedItem().toString();
String value1 = e;
String o = o_code.getSelectedItem().toString();
String value2 = o;
String value3 = o_credit.getText();
// String value4 = session.getText();
// String value5 = designation.getText();
// String value6 = phd_com_id.getText();
String sql = "update passes_optional set o_code='"+value2+"', o_credit='"+value3+"' where e_id='"+value1+"'";
pst = conn.prepareStatement(sql);
pst.execute();
e_id is a drop down list with the values "Item1", "Item2", "Item3", and "Item4". The table contains info on Item1.
That's because with prepareStatement you have to use PreparedStatements.
String sql="update passes_optional set o_code=?, o_credit=? where e_id=?";
PreparedStatement pst=conn.prepareStatement(sql);
pst.setString(1, value2);
pst.setString(2, value3);
pst.setString(3, value1);
pst.execute();
Oracle official Prepared Statements tutorial
UPDATE
As pointed out by JonK, you should also be using pst.executeUpdate() instead of pst.execute():
String sql="update passes_optional set o_code=?, o_credit=? where e_id=?";
PreparedStatement pst=conn.prepareStatement(sql);
pst.setString(1, value2);
pst.setString(2, value3);
pst.setString(3, value1);
pst.executeUpdate();
Related
When I execute this code the error: java.sql.SQLException: Parameter index out of range (4 > number of parameters, which is 3) occurs. What is the issue with this code?
DefaultTableModel RecordTable = (DefaultTableModel)jTable1.getModel();
if (jTable1.getSelectedRowCount()==1){
String type = jComboBox_type.getSelectedItem().toString();
String size =jComboBox_size.getSelectedItem().toString();
String qty = jSpinner1.getValue().toString();
String tot = String.valueOf(total);
String uprice = String.valueOf(price);
finaltotal = finaltotal +total;
jTextField_finaltot.setText(String.valueOf(finaltotal));
RecordTable.setValueAt(type,jTable1.getSelectedRow(),0);
RecordTable.setValueAt(size,jTable1.getSelectedRow(),1);
RecordTable.setValueAt(uprice,jTable1.getSelectedRow(),2);
RecordTable.setValueAt(qty,jTable1.getSelectedRow(),3);
RecordTable.setValueAt(tot,jTable1.getSelectedRow(),4);
String sql ="UPDATE `sales` SET `Type`=?,`Size`=?,`Unit_Price`=?',`Quantity`=?,`Total`=? WHERE `Type`=?";
try {
pst =dbConnection.getConnection().prepareStatement(sql);
pst.setString(1, type);
pst.setString(2, size);
pst.setString(3, uprice);
pst.setString(4, qty);
pst.setString(5, tot);
result =pst.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(Pizza_Menu.class.getName()).log(Level.SEVERE, null, ex);
}
}
You need to remove the single quote after the Unit Price question mark
String sql ="UPDATE `sales` SET `Type`=?,`Size`=?,`Unit_Price`=?',`Quantity`=?,`Total`=? WHERE `Type`=?";
As the comments/answers above suggest, remove the ' quotes.
The ' quotes represent string values in SQL.
The " quotes in SQL represent tables/schemas/columns and so on. Still, you do not need them in your case.
And another error is that you have 6 parameters in your query, but only 5 assignments:
Type=? => pst.setString(1, type);
Size=? => pst.setString(2, size);
Unit_Price=? => pst.setString(3, uprice);
Quantity=? => pst.setString(3, uprice);
Total=? => pst.setString(5, tot);
Type=? => ???? not mapped
So add another pst.setString(6, "Your Value")
String sql = "update Products set ProductName = ?, where ID = ? ";
try{
pst = conn.prepareStatement(sql);
pst.setString(1, Product.getText());
pst.setString(2, ID.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Success");
UpdateJTable();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
Please Again Help me Out of this error. why there's an error on my code.? I follow all the instruction on youtube, but i makes an error. i used 6.9 netbeans.
You added a comma at SQL statement which is not valid.
String sql = "update Products set ProductName = ?, where ID = ? ";
|-remove this comma
Below is my code of a jFrame btn_update:
private void txt_updateActionPerformed(java.awt.event.ActionEvent evt) {
try{
String Value1=txt_Emp_ID.getText();
String Value2=txt_Name.getText();
String Value3=txt_Surname.getText();
String Value4=txt_age.getText();
String Value5=txt_Username.getText();
String Value6=txt_Password.getText();
String sql = "update employee set Emp_ID=?, name=?,surname=?,age=?,username=?,password=?";
pst.setString(1, Value1);
pst.setString(2, Value2);
pst.setString(3, Value1);
pst.setString(4, Value1);
pst.setString(5, Value1);
pst.setString(6, Value1);
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
JOptionPane.showMessageDialog(null, "Updated!!");
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
and my configurations:
Connection conn=null;
ResultSet rs = null;
PreparedStatement pst = null;
when I try to press the update button I get this:
java.sql.SQLException:Parameter index out of Range (2>number of parameters, which is 1)
can someone help me?
A few issues
Your exception message indicates that the PreparedStatement only has 2 parameters. Assign the variable before setting the parameters
Copy/paste error where parameters 3-6 are all Value1 instead of Value3, Value4, etc.
executeQuery is used to query the database. Use executeUpdate for database write operations
Result:
preparesStatement = connection.prepareStatement(sql);
preparesStatement.setString(1, value1);
...// etc.
preparesStatement.setString(6, value6);
I want to insert primary key of selected item of combo box
but I got this error Parameter index out of range(12 > number of parameter which is 0)".
I really don't know how to fix this.
Here is my code:
String valueTrainer = "kosong";
try
{
String sql2 = "Insert into ahli (MemberID, TrainerID, Name, ICNumber, Address, Nationality,"
+ "PhoneNumber, Email, EmergencyPerson, EmergencyContact, DateReg, MemberTypeID) "
+ "values(?,?,?,?,?,?,?,?,?,?,?,?)";
pst = conn.prepareStatement(sql2);
pst.setString(1, MemberIDTextField.getText());
pst.setString(2, valueTrainer);
pst.setString(3, NameTextField.getText());
pst.setString(4, jTextField1.getText());
pst.setString(5, AddressTextArea.getText());
//Nationality combo box
String nationalityList = NationalityComboBox.getSelectedItem().toString();
pst.setString(6, nationalityList);
pst.setString(7, PhoneNumberTextField.getText());
pst.setString(8, EmailTextField.getText());
pst.setString(9, EmerContactPersonTextField.getText());
pst.setString(10, EmerContactNumberTextField.getText());
//Date Chooser
pst.setString(11, ((JTextField)MemberDateChooser.getDateEditor().getUiComponent()).getText());
//membertype combobox
// problem start from here, I think..
String memberTypeList = MemberTypeComboBox.getSelectedItem().toString();
String sql1 ="Select MemberTypeID from jeniskeahlian where Type = '"+memberTypeList+"' " ;
pst = conn.prepareStatement(sql1);
rs = pst.executeQuery();
while(rs.next()){
String memberType = rs.getString("MemberTypeID");
pst.setString(12, memberType);
}
pst.execute();
JOptionPane.showMessageDialog(null, "New member has been added");
} catch (SQLException | HeadlessException e) {
JOptionPane.showMessageDialog(null, e);
}
You are not using pst properly.
same named reference on prepared statement can not be used on different sql statement unless one is used and closed properly. Prepare, set, execute, close on one statement, and then repeat the same on another statement.
Change your code as below.
try
{
String memberTypeList = MemberTypeComboBox.getSelectedItem().toString();
String sql1 ="Select MemberTypeID
from jeniskeahlian
where Type = ? " ;
pst = conn.prepareStatement(sql1);
pst.setString( 1, memberTypeList );
rs = pst.executeQuery();
String memberType = "";
while(rs.next()){
memberType = rs.getString("MemberTypeID");
}
rs.close();
pst.close();
String sql2 = "Insert into ahli (MemberID, TrainerID, Name,
ICNumber, Address, Nationality,
PhoneNumber, Email, EmergencyPerson,
EmergencyContact, DateReg, MemberTypeID)
values(?,?,?,?,?,?,?,?,?,?,?,?)";
pst = conn.prepareStatement(sql2);
pst.setString(1, MemberIDTextField.getText());
pst.setString(2, valueTrainer);
pst.setString(3, NameTextField.getText());
pst.setString(4, jTextField1.getText());
pst.setString(5, AddressTextArea.getText());
//Nationality combo box
String nationalityList = NationalityComboBox.getSelectedItem().toString();
pst.setString(6, nationalityList);
pst.setString(7, PhoneNumberTextField.getText());
pst.setString(8, EmailTextField.getText());
pst.setString(9, EmerContactPersonTextField.getText());
pst.setString(10, EmerContactNumberTextField.getText());
//Date Chooser
pst.setString(11, ((JTextField)MemberDateChooser
.getDateEditor()
.getUiComponent()).getText());
pst.setString(12, memberType);
pst.execute();
JOptionPane.showMessageDialog(null, "New member has been added");
} // try
While your insert statement seems properly populated, in the while-loop you rare trying to execute a select statement which has no parameters, so that pst.setString(12,memberType) fails.
String sql1 ="Select MemberTypeID from jeniskeahlian where Type = '"+memberTypeList+"' " ; //no ? in this query
Before you execute the query you should have set all the values for all the parameters.
whenever i try to insert, delete and update i get the following syntax error. but after I close the exception jdialog and restart the application the data is either inserted, update and deleted
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your mariaDB server for the right syntax to use near 'Customer' at line 1
Insert query
try {
String sql = "Insert into Customer(ID , FirstName, MiddleName, LastName, DOB, Gander, Address, City, PostCode) "
+ "values (?, AES_ENCRYPT(?, 'uk112'),AES_ENCRYPT(?, 'uk112'),"
+ "AES_ENCRYPT(?, 'uk112'),?,AES_ENCRYPT(?, 'uk112'),"
+ "AES_ENCRYPT(?, 'uk112'),AES_ENCRYPT(?, 'uk112'),"
+ "AES_ENCRYPT(?, 'uk112'))";
pst = newconn.prepareStatement(sql);
pst.setString(1, jTextFieldID.getText());
pst.setString(2, jTextFieldFirstname.getText());
pst.setString(3, jTextFieldMiddlename.getText());
pst.setString(4, jTextFieldLastname.getText());
pst.setString(5, ((JTextField)txt_Date.getDateEditor().getUiComponent()).getText());
pst.setString(6, Gander);
pst.setString(7, jTextFieldAddress.getText());
pst.setString(8, jTextFieldCity.getText());
pst.setString(9, jTextFieldPostcode.getText());
pst.execute();
newconn.close();
JOptionPane.showMessageDialog(null, "Saved");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
Update query
try{
String value1 = jTextFieldID.getText();
String value2 = jTextFieldFirstname.getText();
String value3 = jTextFieldMiddlename.getText();
String value4 = jTextFieldLastname.getText();
String value5 = ((JTextField)txt_Date.getDateEditor().getUiComponent()).getText();
String value6= Gander.toString();
String value7= jTextFieldAddress.getText();
String value8 = jTextFieldCity.getText();
String value9 = jTextFieldPostcode.getText();
String sql = "update Customer set FirstName= AES_ENCRYPT(?,'uk112'),"
+ "MiddleName= AES_ENCRYPT(?,'uk112'),"
+ "LastName= AES_ENCRYPT(?,'uk112'),"
+ "DOB=?,Gander= AES_ENCRYPT(?,'uk112'),"
+ "Address= AES_ENCRYPT(?,'uk112'),"
+ "City= AES_ENCRYPT(?,'uk112'),"
+ "PostCode= AES_ENCRYPT(?,'uk112') where ID=?";
pst = newconn.prepareStatement(sql);
pst.setString(1, value2);
pst.setString(2, value3);
pst.setString(3, value4);
pst.setString(4, value5);
pst.setString(5, value6);
pst.setString(6, value7);
pst.setString(7, value8);
pst.setString(8, value9);
pst.setString(9, value1);
pst.execute();
newconn.close();
JOptionPane.showMessageDialog(null, "Data is updated");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
Delete query
String sql = "delete from Customer Where ID =?";
try{
pst= newconn.prepareStatement(sql);
pst.setString(1, jTextFieldID.getText());
pst.execute();
JOptionPane.showMessageDialog(null, "Deleted");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}