Results 1 to 5 of 5
  1. #1
    venkatteshb is offline Member
    Join Date
    Aug 2008
    Posts
    16
    Rep Power
    0

    Default Prob with updating in jdbc

    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    import java.applet.*;
    import javax.swing.*;
    import javax.sql.*;
    import javax.swing.JOptionPane;

    public class upd_demo{

    String support_count = "1";
    String item_name=null;

    int i=0;

    //Parameters for the creation of the database.
    String dbuser = "";
    String dbpasswd = "";
    String DriverPrefix = "jdbc:odbc:";
    String DataSource ="mush";

    //The Updation String
    String upd = "UPDATE supp_count_tab SET upp_count='"+support_count+"' WHERE Items='"+item_name+"'";

    upd_demo(){
    //Loads JDBC/ODBC driver
    try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }
    catch(Exception e)
    {
    //Uses the JFC Swing to display warning message in Option Pane with
    //relevant information about the error.
    JOptionPane.showMessageDialog(null,""+e.getMessage (),"JDBC Driver Error",JOptionPane.WARNING_MESSAGE);
    return;
    }

    Statement stmt = null;
    Connection conn = null;

    //Creates connection to database
    try {
    conn = DriverManager.getConnection(DriverPrefix+DataSourc e,dbuser, dbpasswd);
    stmt = conn.createStatement();
    }
    catch (Exception e)
    {
    JOptionPane.showMessageDialog(null,""+e.getMessage (),"Connection Error",JOptionPane.WARNING_MESSAGE);
    }

    //Updates the database with data
    try {
    for(i=1;i<=119;i++){
    item_name=String.valueOf(i);
    System.out.println(item_name);

    stmt.executeUpdate(upd);
    }

    conn.close();
    // dispose();

    }catch (Exception e) {
    JOptionPane.showMessageDialog(null,"Some Error\n"+e.getMessage(),"Communication Error",JOptionPane.WARNING_MESSAGE);
    }
    }
    public static void main (String[] args) {
    new upd_demo();
    }
    }


    Actually I typed the above codes to update the table a test value "1"

    but it is not getting updated.

    However, if i remove the WHERE condition from

    String upd = "UPDATE supp_count_tab SET upp_count='"+support_count+"' WHERE Items='"+item_name+"'";

    it is working fine.




    and about the table, i am using access database and the table consists of two fields only,

    1.Items
    2.supp_count

    and i have put both the fields to type "text"


    Can anyone suggest what the prob may b?

  2. #2
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    hello,

    I think this is the problem:
    Java Code:
    stmt = conn.createStatement();
    here you need updatable resultset... hope this helps :)

  3. #3
    venkatteshb is offline Member
    Join Date
    Aug 2008
    Posts
    16
    Rep Power
    0

    Default

    Can u please tell me what exactly I should do?

  4. #4
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    Use PreparedStatement

    For more info see the javadoc of PreparedStatement

    (well , I tried to post the javadoc link of PreparedStatement here, but for some reason it didn't working... don't know why ... :()

  5. #5
    venkatteshb is offline Member
    Join Date
    Aug 2008
    Posts
    16
    Rep Power
    0

    Default

    I fixed it, man,
    I just copied and pasted the below statement

    String upd = "UPDATE supp_count_tab SET upp_count='"+support_count+"' WHERE Items='"+item_name+"'";

    just before

    stmt.executeUpdate(upd);

    and it worked.....

    thanks for your reply...

Similar Threads

  1. for each prob
    By kusumathatavarthi in forum New To Java
    Replies: 7
    Last Post: 05-21-2008, 02:00 PM
  2. Replies: 0
    Last Post: 04-01-2008, 11:17 AM
  3. Prob with an exercise
    By jhetfield18 in forum New To Java
    Replies: 4
    Last Post: 02-15-2008, 07:11 PM
  4. having prob with Exception
    By eva in forum New To Java
    Replies: 1
    Last Post: 01-04-2008, 06:44 PM
  5. Replies: 0
    Last Post: 09-28-2007, 01:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •