java.sql.SQLException :no value specified for parameter 6 - java

when press update button it shows:
"java.sql.SQLException:parameter out of range(1>number of parameters,which is 0)".
private void updateActionPerformed(java.awt.event.ActionEvent evt) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/smakdb","root","kisal400");
String sql="Update itemk set name=?,type=?, buying price=?, selling price=?,description=? where itemid=?";
pst=conn.prepareStatement(sql);
pst.setString(1, name2.getText());
String value=type2.getSelectedItem().toString();
pst.setString(2,value);
pst.setDouble(3,Double.parseDouble(buying2.getText()));
pst.setDouble(4,Double.parseDouble(selling2.getText()));
pst.setString(5,descript2.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "updated!!!");
conn.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}

You have 6 question mark in the query, why not set the 6th parameter value?
You need set itemid as well.

In String sql you have six parameters "?" and once set five parameters.

Related

jtextfiled not retrieving the value picked from combobox

I want retrieve the value picked from combo box to jtextfield. As per my UI combobox is in 4th. So I coded:
pst.setString(4, (String)cmbPaySub.getSelectedItem());
and the error pop-up:
Parameter index out of range.(4> number of parameters, which is 1".
I tried by coding;
pst.setString(1, (String)cmbPaySub.getSelectedItem());
Neither error will pop-up and value also not picking up.
private void cmbPaySubActionPerformed(java.awt.event.ActionEvent evt) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sipnena", "root", "");
String sql="select * from payments where cmbSubject=?";
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(4, (String)cmbPaySub.getSelectedItem());
ResultSet rs=pst.executeQuery();
while(rs.next()){
txtFee.setText(rs.getString("Fee"));
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
Kindly help me to retrieve the value to the jtextfield.
When reading the result you're replacing the entire text by the result's value in a loop so only the last value will show:
while(rs.next()){
txtFee.setText(rs.getString("Fee"));
}
You should concatenate all the values, and then set the text:
StringBuilder text = new StringBuilder();
while(rs.next()){
text.append(rs.getString("Fee"));
text.append("\n");
}
txtFee.setText(text.toString());

com.microsoft.sqlserver.jdbc.SQLServerException :The value is not set for the parameter number 4

I am facing the above-mentioned error in the following code. Please help me to fix this.
private void But_AddIncomeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;databasename=DB_Project;user=User;Password=password";
Connection con= DriverManager.getConnection(url);
String query = "insert into tbl_Reg(FullName,CNIC,Email_Address,Pswd,Adrs,PhoneNo)values(?,?,?,?,?,?)";
PreparedStatement pst=con.prepareStatement(query);
pst.setString (1,((JTextField)DateChooser.getDateEditor().getUiComponent()).getText());
pst.setString(2,Amnt_TF.getText());
pst.setString(3,Src_TF.getText());
pst.execute();
JOptionPane.showMessageDialog(null,"Income Detail Updated");
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
}
Your query has 6 parameters (6 ? in SQL) but you set only 3 (with setString() method).
You need to set all 6 to avoid this exception.

Error with Oracle 10g auto-incremented sequence

I created a sequence in Oracle 10g because I have to increase case_number field auto_incremently. But I'm getting an error.
private void button_saveandsubmitActionPerformed(java.awt.event.ActionEvent evt) {
con = JavaConnectDB.ConnectDB();
try{
String sql="insert into FIR_form values(case_number_sequence,?,?,?,?,?,?,?,?,?,?,?)";
pst = (OraclePreparedStatement) con.prepareStatement(sql);
pst.setString(1,text_date.getText());
pst.setString(2,text_district.getText());
pst.setString(3,text_subject.getText());
pst.setString(4,text_description.getText());
pst.setString(5,text_cfullname.getText());
pst.setString(6,text_fhname.getText());
pst.setString(7,text_caddress.getText());
pst.setString(8,text_contact.getText());
pst.setString(9,text_suspectfullname.getText());
pst.setString(10,text_suspectaddress.getText());
pst.setString(11,text_suspectdescription.getText());
rs = (OracleResultSet) pst.executeQuery();
if(rs.next()){
JOptionPane.showMessageDialog(null, "the FIR has been added successfully!!!");
con.close();
this.dispose();
}
else{
JOptionPane.showMessageDialog(null, "enter all fields appropriately first!!!");
}
con.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null, "An error occured. try again later !!!");
}
}
The field in the table will not auto-populate because you have defined a sequence. You must either reference the sequence.nextval in your insert statement to insert the value, or add a trigger to the table to populate the column from the sequence.
See this post for an example:

Java sql execute with more variable

I have a frame with 50 label, i can select them, for example i select A1,A2,A3,A4 the program save these in string (String=A1;A2;A3;A4) and i want to call in this query
try{
String sql= "UPDATE Teremszekek SET Állapot='Foglalt' where Terem='1' and Szék='A1' and DKód='1' ";
PreparedStatement pst=conn.prepareStatement(sql);
pst.execute();
}catch(Exception e){
System.out.println(e);
}
}
Állapot=condition(available,unavailable) Terem=room(index of room) Szék=chair DKód=Date code
How can i write that if i choose, A1,A2,A3,A4 etc label then it paste to query
//i select them at the same time
If I understand your question in the right way, this should be your solution:
String sql= "UPDATE Teremszekek SET Állapot='Foglalt' where Terem='1' and Szék=? and DKód='1' ";
pst.setString(1, yourValue);

Updating Values Netbeans

I am trying to Update my data from my netbeans to sqlite. there is no problem with the query but when I run the program a message box will appear "java.sql.SQLException:query does not return results". What seems to be the problem?
try{
String value1=txtId.getText();
String value2=txtFirst.getText();
String value3=txtLast.getText();
String value4=txtUser.getText();
String value5=txtPass.getText();
String sql="Update account set id='"+value1+"', fname='"+value2+"', lname='"+value3+"',username='"+value4+"', password='"+value5+"' where id='"+value1+"' ";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
JOptionPane.showMessageDialog(null,"Data Updated");
}
catch(SQLException e){
JOptionPane.showMessageDialog(null,e);
}
int updateCount = pst.executeUpdate();
Instead of executeQuery.
Update query:
st.executeUpdate("update reservation set busname='"+jTextField10.getText()+"',busno='"+jTextField9.getText()+"',cusname='"+jTextField8.getText()+"',noofpass='"+jTextField7.getText()+"',amount='"+jTextField6.getText()+"' where cusname='"+jTextField8.getText()+"' ");
You can also try:
String strQuery = ("update visitor set name='"+jTextField10.getText()+"',bus_no='"+jTextField9.getText()+"',cus_name='"+jTextField8.getText()+"',Date='"+jTextField7.getText()+"',amount='"+jTextField6.getText()+"' where _ID='"+jTextField8.getText()+"' ");

Categories

Resources