Results 1 to 8 of 8

Thread: JDBC problem

  1. #1
    juhiswt is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default JDBC problem

    I m trying to insert values in a table.....connection is established but its not updating the table.
    I m using following code.
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con= DriverManager.getConnection("jdbc:odbc:sms");
    System.out.println("Connected to database");
    Statement stmt=con.createStatement();
    int count=stmt.executeUpdate("insert into Student (Enrollnumber,stuname,mothername,fathername,addrs ,phn_number,mob_number , class)
    values ('"+jTextField1.getText()+"','"+jTextField2.getTex t()+"','"+jTextField3.getText()+"','"+jTextField4. getText()+"',
    '"+jTextField5.getText()+"','"+jTextField6.getText ()+"','"+jTextField7.getText()+"','"+jTextField8.g etText()+"') ");
    }
    catch(Exception e)
    {}

    }
    Last edited by juhiswt; 06-12-2011 at 11:38 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    How do you *know* the connection is established?

    Java Code:
    catch(Exception e)
    {}
    This is not good. If an exception happens you want to know about it. Print a stack trace in the catch block. (See, eg, the Throwable method printStackTrace())

    ---------------

    If you have a problem consider writing a small example that compiles and illustrates the problem. It doesn't need to be a huge program with all sorts of GUI that is *not* causing a problem and NB inspired variable names like jImpossiblyLongClassName666: just a simple main() method that shows connecting to a database trying to update it and failing to get the data back again (or whatever other thing you mean by "not updating"). See SSCCE.

  3. #3
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    How do you know the connection gets established? I dont see anywhere a url,username and pass.

    Your try..catch block is useless, because the catch does nothing. In your catch block add: e.printStackTrace();

    If any exceptions are thrown you will see them.

    I would also suggest instead of using conn.executeUpdate() to use conn.executeQuery();

    Hope this helps

    P.S. Please use code tags
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  4. #4
    juhiswt is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    I have changed as u told me to use executeQuery
    ResultSet rs =stmt.executeQuery("insert into Student (Enrollnumber,stuname,mothername,fathername,addrs ,phn_number,mob_number , class)
    values ('"+Integer.parseInt(jTextField1.getText())+"','"+ jTextField2.getText()+"','"+jTextField3.getText()+ "',
    '"+jTextField4.getText()+"','"+jTextField5.getText ()+"','"+Integer.parseInt(jTextField6.getText())+" ',
    '"+Integer.parseInt(jTextField7.getText())+"','"+j TextField8.getText()+"') ");
    }
    catch(Exception e)
    {
    System.err.println("Exception: "+e.getMessage());

    }

    }
    But now its giving the exception no resultset was produced

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I would also suggest instead of using conn.executeUpdate() to use conn.executeQuery();
    Why?

    Printing count as returned by executeUpdate() seems a good idea though.

  6. #6
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Quote Originally Posted by juhiswt View Post
    But now its giving the exception no resultset was produced
    Of course there will be no ResultSet. you are Inserting data not Selecting it.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  7. #7
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Why?

    Printing count as returned by executeUpdate() seems a good idea though.
    Yeah, my bad. Didn't notice the int count.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  8. #8
    juhiswt is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

Similar Threads

  1. JDBC Problem
    By nitishjtm in forum JDBC
    Replies: 4
    Last Post: 11-18-2010, 07:25 PM
  2. Problem with Jdbc
    By enjoybullife in forum Java Servlet
    Replies: 4
    Last Post: 05-21-2010, 11:19 AM
  3. jdbc problem!!! pls help
    By dilanka007 in forum New To Java
    Replies: 5
    Last Post: 01-16-2010, 06:07 PM
  4. problem with jdbc in jsp
    By nishant.4545 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 08-29-2009, 02:31 PM
  5. JDBC problem
    By peiceonly in forum Threads and Synchronization
    Replies: 2
    Last Post: 08-03-2007, 02:42 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
  •