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()+"' ");
Related
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.
I am working on Java Application and trying so hard to update record in SQLite database but it doesn't work .. btw it doesn't give me any exceptions or errors
String sql="update Food_Fresh set available=? where Type_ID =?";
st=con.prepareStatement(sql);
st.setInt(1, 1);
st.setInt(2, num);
st.executeUpdate();
st.close();
What's the problem ?!
UPDATE
yes , the initialization of sql
try{
Class.forName("org.sqlite.JDBC");
Connection con=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Shatha2012\\Desktop\\Core\\IT\\Graduation Project\\Code\\New folder\\Food\\src\\Food\\Food.sqlite");
JOptionPane.showMessageDialog(null, "DONE");
return con;
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
return null;
}
}
and the committing is set as auto commit
Maybe there are no records in your database with Type_ID = num?
Check the value returned from executeUpdate()
int i = st.executeUpdate();
it will show you the number of records updated
whats wrong with my codes? I think its in the SQL but I don't know why. thank you in advance
try{
clsConnect c = new clsConnect();
Connection conn = c.makeConnection();
Statement statement = conn.createStatement();
String display = "SELECT SUM(Amountpaid) from mofficetbl";
ResultSet rs = statement.executeQuery(display);
while(rs.next()){
totalTF.setText(rs.getString("Amountpaid"));
}
}
catch (SQLException e) {
JOptionPane.showMessageDialog(null, e);
}
when you call this select statement; the returned column is actually called "SUM(amountpaid)" and not amountpaid so either change its alias with
"SELECT SUM(Amountpaid) as Amountpaid from mofficetbl"
You can also do:
rs.getString("Sum(Amountpaid)");
You are selecting SUM(amountpaid) and trying to access the column amountpaid, but that column doesn't actually exist in the ResultSet Try rs.getString(1) or giving the SUM a name in the select as SELECT SUM(amountpaid) as sum from msofficetbl then doing rs.getString("sum");
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);
I m getting except1 on running this code.Please see if there is any mistake within the try block....
Try
{
pst=con.prepareStatement("SELECT Name,Roll,Semester,Address,Phoneno," +
"E-mailId,Gender,DOB,Result FROM stud WHERE Roll = ?");
pst.setString(1,s2);
ResultSet rs=pst.executeQuery();
while(rs.next())
{
s2=rs.getString("Roll");
String s1=rs.getString("Name");
String s3=rs.getString("Semester");
String s4=rs.getString("Address");
String s5=rs.getString("Phoneno");
String s6=rs.getString("E-mailId");
String s7=rs.getString("Gender");
String s8=rs.getString("DOB");
String s9=rs.getString("Result");
t1.setText(s1);
t2.setText(s2);
t3.setText(s3);
t4.setText(s4);
t5.setText(s5);
t6.setText(s6);
t7.setText(s7);
t8.setText(s8);
t9.setText(s9);
}
con.commit();
con.close();
}
catch(SQLException e2)
{
System.out.println("except1");
}
caveat: my Java is rusty -
don't know if field names can contain hypens, depends on the database
print the exact exception that you're getting
why are you doing a commit on a SELECT?
Make Sure data type of "Roll" Atrribute in Database is Character(n).
if it is integer/number then use this
pst.setInt(1,Integer.parseInt(s2));
Make sure you use VARCHAR data type for all attributes in database. If it is not the case then change your code according to these data types.
And print exception stack trace (e2.printStackTrace()) in catch block for getting exact reason for the exception.
Thanks
Try
{
pst=con.prepareStatement("SELECT Name,Roll,Semester,Address,Phoneno," +
"E-mailId,Gender,DOB,Result FROM stud WHERE Roll = ?");
pst.setString(1,s2);
string s2 = '123123'; //pass the required value to Query
ResultSet rs=pst.executeQuery();
while(rs.next())
{
//String s2=rs.getString("Roll");
String s1=rs.getString("Name");
String s3=rs.getString("Semester");
String s4=rs.getString("Address");
String s5=rs.getString("Phoneno");
String s6=rs.getString("E-mailId");
String s7=rs.getString("Gender");
String s8=rs.getString("DOB");
String s9=rs.getString("Result");
t1.setText(s1);
t2.setText(s2);
t3.setText(s3);
t4.setText(s4);
t5.setText(s5);
t6.setText(s6);
t7.setText(s7);
t8.setText(s8);
t9.setText(s9);
}
con.commit(); // use commit only when you are doing create/update operations
con.close();
}
catch(SQLException e2)
{
System.out.println("Error Information");
e2.printStackTrace();// this method display the error information
}