problem in inserting record in ms access.

Discussion in 'Java' started by Navnath Gadakh, Oct 4, 2012.

  1. package javaapplication3;
    import java.sql.*;

    public class JavaApplication3 {
    Connection con;
    Statement st;
    ResultSet rs;

    public JavaApplication3()
    {
    connect();
    }

    public void connect()
    {
    try
    {
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    Class.forName(driver);

    String db = "jdbc:eek:dbc:db1";
    con = DriverManager.getConnection(db);
    st = con.createStatement();
    String sql = "select * from Table1";
    rs = st.executeQuery(sql);

    while(rs.next())
    {
    String fname = rs.getString("fname");
    String lname = rs.getString("lname");
    String address = rs.getString("address");
    String email = rs.getString("email");
    String mobile = rs.getString("mobile");

    System.out.println(fname+lname+address+email+mobile);
    }

    }catch(Exception ex)
    {

    }

    try
    {
    rs.moveToInsertRow();
    rs.updateString("fname","abc");
    rs.updateString("lname","xyz");
    rs.updateString("address","mubmai");
    rs.updateString("email","");
    rs.updateString("mobile","99854874154");
    rs.insertRow();

    st.close();
    rs.close();

    }
    catch(Exception err)
    {
    System.out.println("Error!!!");
    }


    }
    public static void main(String[] args) {
    // TODO code application logic here
    new JavaApplication3();
    }
    }
    Navnath Gadakh, Oct 4, 2012
    #1
    1. Advertising

  2. Navnath Gadakh

    Arne Vajhøj Guest

    On 10/4/2012 12:47 AM, Navnath Gadakh wrote:
    > package javaapplication3;
    > import java.sql.*;
    >
    > public class JavaApplication3 {
    > Connection con;
    > Statement st;
    > ResultSet rs;


    Make them private.

    > public JavaApplication3()
    > {
    > connect();
    > }
    >
    > public void connect()
    > {
    > try
    > {
    > String driver = "sun.jdbc.odbc.JdbcOdbcDriver";


    I will not recommend the JDBC ODBC bridge unless you are absolutely
    forced to use it.

    There are plenty of alternatives.

    > Class.forName(driver);
    >
    > String db = "jdbc:eek:dbc:db1";
    > con = DriverManager.getConnection(db);
    > st = con.createStatement();


    As Martin has explained then this is very likely the cause of
    your specific problem.

    > String sql = "select * from Table1";
    > rs = st.executeQuery(sql);
    >
    > while(rs.next())
    > {
    > String fname = rs.getString("fname");
    > String lname = rs.getString("lname");
    > String address = rs.getString("address");
    > String email = rs.getString("email");
    > String mobile = rs.getString("mobile");
    >
    > System.out.println(fname+lname+address+email+mobile);
    > }
    >
    > }catch(Exception ex)
    > {


    Always print the exception.

    > }
    >
    > try
    > {
    > rs.moveToInsertRow();
    > rs.updateString("fname","abc");
    > rs.updateString("lname","xyz");
    > rs.updateString("address","mubmai");
    > rs.updateString("email","");
    > rs.updateString("mobile","99854874154");
    > rs.insertRow();


    I would suggest using plain INSERT instead of this.

    > st.close();
    > rs.close();
    >
    > }
    > catch(Exception err)
    > {
    > System.out.println("Error!!!");


    Always ...

    > }
    >
    >
    > }
    > public static void main(String[] args) {
    > // TODO code application logic here
    > new JavaApplication3();


    Don't do such heavy work in the constructor.

    > }
    > }


    Arne
    Arne Vajhøj, Oct 4, 2012
    #2
    1. Advertising

  3. Navnath Gadakh

    Roedy Green Guest

    On Wed, 3 Oct 2012 21:47:34 -0700 (PDT), Navnath Gadakh
    <> wrote, quoted or indirectly quoted someone
    who said :

    >
    >package javaapplication3;


    It is traditional to:

    1. ask a question
    2. describe what you expected the code to do.
    3. describe what the code actually did.
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    The iPhone 5 is a low end Rolex.
    Roedy Green, Oct 5, 2012
    #3
  4. Navnath Gadakh

    Lew Guest

    Roedy Green wrote:
    > It is traditional to:
    >
    > 1. ask a question


    For good advice in this area see
    http://www.catb.org/esr/faqs/smart-questions.html

    This is pretty much the canon of Usenet (and everywhere else) request protocol.

    > 2. describe what you expected the code to do.
    > 3. describe what the code actually did.


    Roedy's excellent point about this tradition is rooted in pragmatics.

    It is difficult for those motivated to help you if we have not enough data with which
    to make an assessment.

    Another good source of advice on how to ask code questions:

    http://sscce.org/

    Roedy's mindprod.com also covers this advice.

    --
    Lew
    Lew, Oct 5, 2012
    #4
  5. On 10/4/2012 7:24 PM, Martin Gregorie wrote:
    > On Thu, 04 Oct 2012 17:35:39 -0400, Arne Vajhøj wrote:
    >> On 10/4/2012 12:47 AM, Navnath Gadakh wrote:
    >> I will not recommend the JDBC ODBC bridge unless you are absolutely
    >> forced to use it.
    >>

    > The OP mentioned (in the title) that he is using MS Access. Is there a
    > JDBC driver for it?


    No.

    But there are plenty of other database.

    Going back I can see that I wrote something utterly incomplete.

    When I said alternatives it was alternative databases not
    JDBC drivers.

    Egg on my face.

    Arne
    Arne Vajhøj, Oct 6, 2012
    #5
  6. On 10/6/2012 7:36 AM, Martin Gregorie wrote:
    > On Fri, 05 Oct 2012 20:27:11 -0400, Arne Vajhøj wrote:
    >> But there are plenty of other database.
    >>

    > Indeed, unless the OP was told to use Access (but why, when Derby is
    > freely available and has the advantage of being written in Java?).


    And if one does not like JavaDB/Derby, then there is MySQL, PostgreSQL,
    H2 and FireBird - plus the free editions of Oracle, DB2 and SQLServer.

    > I've
    > had to fix Access systems in the past and remain unimpressed with it.


    The Jet database engine was never anything special. The special was the
    integrated application building tool. But that does not help when
    writing the app in Java anyway.

    Arne
    Arne Vajhøj, Oct 6, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shapper
    Replies:
    2
    Views:
    570
    Paul Clement
    Jul 7, 2005
  2. kalib70
    Replies:
    0
    Views:
    397
    kalib70
    Mar 28, 2006
  3. jeffhg582003

    Inserting record with Microsoft Access

    jeffhg582003, Feb 9, 2006, in forum: Python
    Replies:
    3
    Views:
    386
    Tim Roberts
    Feb 11, 2006
  4. Albert Leibbrandt

    Re: Inserting record with Microsoft Access

    Albert Leibbrandt, Feb 9, 2006, in forum: Python
    Replies:
    1
    Views:
    475
    Dennis Lee Bieber
    Feb 9, 2006
  5. Steve Holden
    Replies:
    2
    Views:
    419
    Chris Smith
    Feb 12, 2006
Loading...

Share This Page