Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

java and jdbc

  • 28-04-2004 02:59PM
    #1
    Closed Accounts Posts: 34


    hi guys

    i am trying to use this java code to get access to a ms access database file called fred, stored in the root of the c: drive.

    i get a sql error when i run it in jbuilder.?


    any tips?


    __________________________________________________________________



    package jdbc;

    import javax.swing.UIManager;
    import sun.jdbc.odbc.*;
    import java.sql.*;
    import sun.jdbc.odbc.*;


    public class JdbcTest {

    public JdbcTest() {
    try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:MS Access Database;DBQ=c:\\fred.mdb", "", "");
    System.out.println("connected ok");

    Statement stmt = con.createStatement();
    // putting data into the database
    String insertSQL="insert into Coffee (name, price, quantity, sales) values ('greg',12,12,5 )";
    stmt.executeUpdate(insertSQL);

    // getting data from the database
    String selectSQL="select * from Coffee where sales < 9";
    ResultSet rs = stmt.executeQuery(selectSQL);
    while (rs.next()){
    String name = rs.getString("name");
    System.out.println(name);
    }

    }
    catch(Exception ee)
    {
    System.out.println("ERROR connecting:"+ee);
    }


    }

    public static void main(String[] args) {
    JdbcTest jdbcTest1 = new JdbcTest();
    }
    }


Comments

  • Registered Users, Registered Users 2, Paid Member Posts: 21,273 ✭✭✭✭Stark


    You might want to post the error.


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    yea the full stack trace would help.


  • Registered Users, Registered Users 2 Posts: 1,505 ✭✭✭viking


    You may need to create a DataSource Name (DSN) in the 32-bit ODBC control panel in order to connect to the Mdb file rather than using the absolute path the file (ie. c:\\fred.mdb)

    e.g jdbc:odbc:<DSN>

    viking

    PS. I may be wrong, its been a while...


  • Registered Users, Registered Users 2 Posts: 678 ✭✭✭briano


    I have a JSP that also uses an Ms Access DB.

    When I am connecting to it I had to use the string:

    "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\\Apache\Tomcat\webapps\ass2\Shopping.mdb"

    This works without a DSN

    The only differences is the parenthisis and the spelling though.

    Also, you should probably close the result set, statement and connection when you are finished with them.


  • Registered Users, Registered Users 2 Posts: 261 ✭✭HaVoC


    i just did a project with jdbc and ms access

    viking is right you need to crete a dsn in Data Sources (Odbc)

    and use the name in the program
    or at least that how i did it.

    ie
    Connection con ;



    public void Open()
    {
    try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    //"mydatabase" is name of ur dsn that u setup
    con = DriverManager.getConnection("jdbc:odbc:mydatabase");
    }
    catch(Exception e)
    {}

    }


  • Advertisement
Advertisement