Connect to access

Discussion in 'Java' started by morc, May 24, 2006.

  1. morc

    morc Guest

    Hi,
    i haven't been doing database related programming for a large period of
    time.
    I have forgotten how to connect to a databse.
    if someone could please do me the favour of giving me an example of
    how to connect to access db and run a query.

    thanks in adavnce
    -morc
    morc, May 24, 2006
    #1
    1. Advertising

  2. morc

    Rhino Guest

    "morc" <> wrote in message
    news:...
    > Hi,
    > i haven't been doing database related programming for a large period of
    > time.
    > I have forgotten how to connect to a databse.
    > if someone could please do me the favour of giving me an example of
    > how to connect to access db and run a query.
    >


    Here are some fragments that should answer your questions; your code simply
    needs to invoke the three methods at the appropriate time. Naturally, the
    sequence should be that you load the JDBC driver first, then get a
    connection to the database, then query or update the database. You'll also
    want to define some variables as follows:

    final String CLASS_NAME = getClass().getName();
    Connection conn01 = null;
    static final String DEMO_TABLE = "RHINO.PERSONNEL";

    public void loadDriver() {

    String METHOD_NAME = "loadDriver()";

    /* Initialize the variable that contains the name of the JDBC
    driver. */
    String jdbcDriverName = "com.ibm.db2.jcc.DB2Driver";

    /* Load the JDBC driver. */
    try {
    Class.forName(jdbcDriverName);
    } catch (ClassNotFoundException excp) {
    System.err.println(CLASS_NAME + "." + METHOD_NAME + " -
    Encountered ClassNotFoundException while attempting to load JDBC driver " +
    jdbcDriverName + ". Error: " + excp);
    excp.printStackTrace();
    System.exit(16);
    }
    }

    public void connectToDatabase() {

    String METHOD_NAME = "connectToDatabase()";

    /* Initialize the variables used to get the connection. */
    String databaseName = "sample";
    String url = "jdbc:db2:" + databaseName;
    String loginName = "admin";
    String password = "adminpw";

    /* Connect to the database. */
    try {
    conn01 = DriverManager.getConnection(url, loginName, password);
    } catch (SQLException sql_excp) {
    System.err.println(CLASS_NAME + "." + METHOD_NAME
    + " - Encountered SQLException on connect to URL " + url
    + ". Error: "
    + sql_excp);
    sql_excp.printStackTrace();
    System.exit(16);
    }

    /* Set autocommit off. */
    try {
    conn01.setAutoCommit(false);
    System.out.println("Turn off autocommit...");
    } catch (SQLException sql_excp) {
    System.err.println(CLASS_NAME + "." + METHOD_NAME
    + " - Encountered SQLException on attempt to turn
    autocommit off. Error: "
    + sql_excp);
    sql_excp.printStackTrace();
    System.exit(16);
    }
    }

    public void queryTable() {

    String METHOD_NAME = "queryTable()";

    System.out.println("Current D21 rows in the table: ");
    String queryTableSQL = "select lastname, workdept, salary, hiredate
    from "
    + DEMO_TABLE + " where workdept = 'D21' ";

    /*
    * Query the demonstration table to get information about certain
    * employees.
    */
    Statement queryTableStmt = null;
    ResultSet rs01 = null;
    try {
    queryTableStmt = conn01.createStatement();
    rs01 = queryTableStmt.executeQuery(queryTableSQL);
    } catch (SQLException excp) {
    System.err.println(CLASS_NAME + "." + METHOD_NAME
    + " - Encountered SQLException while trying to get
    information from "
    + DEMO_TABLE + " table. Error: " + excp);
    excp.printStackTrace();
    System.exit(16);
    }

    /* Initialize the host variables used for handling the result set.
    */
    String lastname = null;
    String workdept = null;
    BigDecimal salary = null;
    java.sql.Date hiredate;

    /*
    * Print each line of the result set. The salary, which is a double,
    is
    * converted to a BigDecimal and then copied to another BigDecimal
    that
    * has exactly 2 decimal places.
    */
    try {
    while (rs01.next()) {
    lastname = rs01.getString(1);
    workdept = rs01.getString(2);
    salary = rs01.getBigDecimal(3);
    hiredate = rs01.getDate(4);
    System.out.println(lastname + " " + workdept + " " +
    salary.toString() + " "
    + hiredate.toString());
    }
    } catch (SQLException sql_excp) {
    System.err.println(CLASS_NAME + "." + METHOD_NAME
    + " - Encountered SQLException while reading " +
    demoTable
    + " table. Error: " + sql_excp);
    sql_excp.printStackTrace();
    System.exit(16);
    }

    /* Close the result set, dispose of the statement, and commit. */
    try {
    rs01.close();
    queryTableStmt.close();
    conn01.commit();
    } catch (SQLException sql_excp) {
    System.err.println(CLASS_NAME + "." + METHOD_NAME
    + " - Encountered SQLException while closing " +
    demoTable
    + " result set, closing statement, or committing. Error:
    " + sql_excp);
    sql_excp.printStackTrace();
    System.exit(16);
    }
    }


    --
    Rhino
    Rhino, May 24, 2006
    #2
    1. Advertising

  3. morc

    morc Guest

    thanks alot this is what i was looking for
    stupid question:
    how can i find the info on my current jdbc driver? and if i don't have
    one where do i get it and how to installs it.

    Im not familiar with doing this so all help is appreciated.
    -morc
    morc, May 24, 2006
    #3
  4. morc

    morc Guest

    public void connect(){

    try {
    Class.forName("sun.jdbc.odbc.JbdcOdbcDriver");
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }

    String databaseName = "invent";
    String url = "jdbc:eek:dbc:" + databaseName;
    String loginName = "Admin";
    String password = "lace";

    try {
    conn01 = DriverManager.getConnection(url, loginName, password);
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

    this is the code i used.
    this is the error i get:
    java.lang.ClassNotFoundException: sun.jdbc.odbc.JbdcOdbcDriver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at allm.test.DBaccess.connect(DBaccess.java:18)
    at allm.test.test.main(test.java:17)
    java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at allm.test.DBaccess.connect(DBaccess.java:29)
    at allm.test.test.main(test.java:17)


    can somebody please pooint me in the right direction.
    do i need to configure somethign in access? is there any thing i have
    to do in the ODBC configurations?
    morc, May 24, 2006
    #4
  5. morc

    Rhino Guest

    "morc" <> wrote in message
    news:...
    > thanks alot this is what i was looking for
    > stupid question:
    > how can i find the info on my current jdbc driver? and if i don't have
    > one where do i get it and how to installs it.
    >
    > Im not familiar with doing this so all help is appreciated.
    > -morc
    >

    According to the post which shows your error message, you appear to be using
    the Sun JDBC/ODBC Driver. That driver can be found in the rt.jar file. If
    you have a JDK installed on your machine, rt.jar can be found in the jre\lib
    folder. If you have a JRE installed on your machine, you should find it in
    the lib folder. As long as you have the JDK or the JRE in your PATH, your
    Java program should be able to find it and use it.

    I'm going to do a few tests just to verify this on my machine, then I'll
    post back. This is just a quick note to let you know that I'm working on the
    problem.

    --
    Rhino
    Rhino, May 24, 2006
    #5
  6. morc

    Rhino Guest

    "Rhino" <> wrote in message
    news:e52h64$fc3$...
    >
    > "morc" <> wrote in message
    > news:...
    >> thanks alot this is what i was looking for
    >> stupid question:
    >> how can i find the info on my current jdbc driver? and if i don't have
    >> one where do i get it and how to installs it.
    >>
    >> Im not familiar with doing this so all help is appreciated.
    >> -morc
    >>

    > According to the post which shows your error message, you appear to be
    > using the Sun JDBC/ODBC Driver. That driver can be found in the rt.jar
    > file. If you have a JDK installed on your machine, rt.jar can be found in
    > the jre\lib folder. If you have a JRE installed on your machine, you
    > should find it in the lib folder. As long as you have the JDK or the JRE
    > in your PATH, your Java program should be able to find it and use it.
    >
    > I'm going to do a few tests just to verify this on my machine, then I'll
    > post back. This is just a quick note to let you know that I'm working on
    > the problem.
    >


    Sorry, I thought that should work but when I tried it, I got the same
    problem you did: ClassNotFoundException when I tried to load the driver. I
    had rt.jar in the Build Path (the Eclipse equivalent of Classpath) and even
    moved it to the top but it made no difference. For some reason, it's not
    seeing the driver and I can't think of anything else to try offhand.

    Why don't you try going to the comp.lang.java.databases newsgroup? Try a
    Google Groups search on Access or the driver name first since someone must
    have had and solved this problem before. If that doesn't work, try a new
    post and explain your problem.

    Post back here when you have the answer; now that I've spent time on your
    problem, I'm interested in hearing the solution ;-)

    --
    Rhino
    Rhino, May 24, 2006
    #6
  7. morc

    IchBin Guest

    morc wrote:
    > Hi,
    > i haven't been doing database related programming for a large period of
    > time.
    > I have forgotten how to connect to a databse.
    > if someone could please do me the favour of giving me an example of
    > how to connect to access db and run a query.
    >
    > thanks in adavnce
    > -morc
    >


    There was just that same discussion, in the last hour, funny enough it
    is in the DATABASE NG, alt.comp.lang.java.databases

    Look at
    http://www.javaworld.com/javaworld/javaqa/2000-09/03-qa-0922-access.html

    Thanks in Advance...
    IchBin, Pocono Lake, Pa, USA
    http://weconsultants.servebeer.com/JHackerAppManager
    __________________________________________________________________________

    'If there is one, Knowledge is the "Fountain of Youth"'
    -William E. Taylor, Regular Guy (1952-)
    IchBin, May 24, 2006
    #7
  8. morc

    IchBin Guest

    IchBin wrote:
    > morc wrote:
    >> Hi,
    >> i haven't been doing database related programming for a large period of
    >> time.
    >> I have forgotten how to connect to a databse.
    >> if someone could please do me the favour of giving me an example of
    >> how to connect to access db and run a query.
    >>
    >> thanks in adavnce
    >> -morc
    >>

    >
    > There was just that same discussion, in the last hour, funny enough it
    > is in the DATABASE NG, alt.comp.lang.java.databases
    >
    > Look at
    > http://www.javaworld.com/javaworld/javaqa/2000-09/03-qa-0922-access.html
    >


    I Should have read this thread first before I posted... This was
    answered in alt.comp.lang.java.databases. The OP has failed to mention
    he is working two NG's.

    He had miss spelled the the driver name.

    Thanks in Advance...
    IchBin, Pocono Lake, Pa, USA
    http://weconsultants.servebeer.com/JHackerAppManager
    __________________________________________________________________________

    'If there is one, Knowledge is the "Fountain of Youth"'
    -William E. Taylor, Regular Guy (1952-)
    IchBin, May 24, 2006
    #8
  9. Rhino wrote:
    > "Rhino" <> wrote in message
    > news:e52h64$fc3$...
    >> "morc" <> wrote in message
    >> news:...
    >>> thanks alot this is what i was looking for
    >>> stupid question:
    >>> how can i find the info on my current jdbc driver? and if i don't have
    >>> one where do i get it and how to installs it.
    >>>
    >>> Im not familiar with doing this so all help is appreciated.
    >>> -morc
    >>>

    >> According to the post which shows your error message, you appear to be
    >> using the Sun JDBC/ODBC Driver. That driver can be found in the rt.jar
    >> file. If you have a JDK installed on your machine, rt.jar can be found in
    >> the jre\lib folder. If you have a JRE installed on your machine, you
    >> should find it in the lib folder. As long as you have the JDK or the JRE
    >> in your PATH, your Java program should be able to find it and use it.
    >>
    >> I'm going to do a few tests just to verify this on my machine, then I'll
    >> post back. This is just a quick note to let you know that I'm working on
    >> the problem.
    >>

    >
    > Sorry, I thought that should work but when I tried it, I got the same
    > problem you did: ClassNotFoundException when I tried to load the driver. I
    > had rt.jar in the Build Path (the Eclipse equivalent of Classpath) and even
    > moved it to the top but it made no difference. For some reason, it's not
    > seeing the driver and I can't think of anything else to try offhand.
    >
    > Why don't you try going to the comp.lang.java.databases newsgroup? Try a
    > Google Groups search on Access or the driver name first since someone must
    > have had and solved this problem before. If that doesn't work, try a new
    > post and explain your problem.
    >
    > Post back here when you have the answer; now that I've spent time on your
    > problem, I'm interested in hearing the solution ;-)
    >
    > --
    > Rhino
    >
    >

    Don't forget that if you're using ODBC you need to set up a DSN that
    maps the dbname to a real database.

    --
    TechBookReport Java http://www.techbookreport.com/JavaIndex.html
    TechBookReport, May 25, 2006
    #9
  10. morc

    Rhino Guest

    "TechBookReport" <> wrote in message
    news:...
    > Rhino wrote:
    >> "Rhino" <> wrote in message
    >> news:e52h64$fc3$...
    >>> "morc" <> wrote in message
    >>> news:...
    >>>> thanks alot this is what i was looking for
    >>>> stupid question:
    >>>> how can i find the info on my current jdbc driver? and if i don't have
    >>>> one where do i get it and how to installs it.
    >>>>
    >>>> Im not familiar with doing this so all help is appreciated.
    >>>> -morc
    >>>>
    >>> According to the post which shows your error message, you appear to be
    >>> using the Sun JDBC/ODBC Driver. That driver can be found in the rt.jar
    >>> file. If you have a JDK installed on your machine, rt.jar can be found
    >>> in the jre\lib folder. If you have a JRE installed on your machine, you
    >>> should find it in the lib folder. As long as you have the JDK or the JRE
    >>> in your PATH, your Java program should be able to find it and use it.
    >>>
    >>> I'm going to do a few tests just to verify this on my machine, then I'll
    >>> post back. This is just a quick note to let you know that I'm working on
    >>> the problem.
    >>>

    >>
    >> Sorry, I thought that should work but when I tried it, I got the same
    >> problem you did: ClassNotFoundException when I tried to load the driver.
    >> I had rt.jar in the Build Path (the Eclipse equivalent of Classpath) and
    >> even moved it to the top but it made no difference. For some reason, it's
    >> not seeing the driver and I can't think of anything else to try offhand.
    >>
    >> Why don't you try going to the comp.lang.java.databases newsgroup? Try a
    >> Google Groups search on Access or the driver name first since someone
    >> must have had and solved this problem before. If that doesn't work, try a
    >> new post and explain your problem.
    >>
    >> Post back here when you have the answer; now that I've spent time on your
    >> problem, I'm interested in hearing the solution ;-)
    >>
    >> --
    >> Rhino

    > Don't forget that if you're using ODBC you need to set up a DSN that maps
    > the dbname to a real database.
    >

    Yes, I expected to have to do that. But it doesn't explain why we get the
    ClassNotFoundException while trying to load the driver.

    --
    Rhino
    Rhino, May 25, 2006
    #10
  11. morc

    morc Guest

    I found the error it was a simple spelling mistgake
    thx for ur help
    morc, May 25, 2006
    #11
  12. morc

    morc Guest

    I found the error it was a simple spelling mistgake
    thx for ur help
    morc, May 25, 2006
    #12
    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. Otis Mukinfus
    Replies:
    3
    Views:
    904
    Juan T. Llibre
    Jul 8, 2005
  2. Replies:
    3
    Views:
    40,178
    Roedy Green
    Nov 16, 2005
  3. Replies:
    2
    Views:
    272
  4. Mohsen Pahlevanzadeh

    Qt connect and first connect or unicode

    Mohsen Pahlevanzadeh, Sep 17, 2013, in forum: Python
    Replies:
    3
    Views:
    147
    Mohsen Pahlevanzadeh
    Sep 18, 2013
  5. Mohsen Pahlevanzadeh

    Re: Qt connect and first connect or unicode

    Mohsen Pahlevanzadeh, Sep 17, 2013, in forum: Python
    Replies:
    0
    Views:
    111
    Mohsen Pahlevanzadeh
    Sep 17, 2013
Loading...

Share This Page