Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

java and jdbc

Options
  • 28-04-2004 3: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 Posts: 20,941 ✭✭✭✭Stark


    You might want to post the error.

    ⛥ ̸̱̼̞͛̀̓̈́͘#C̶̼̭͕̎̿͝R̶̦̮̜̃̓͌O̶̬͙̓͝W̸̜̥͈̐̾͐Ṋ̵̲͔̫̽̎̚͠ͅT̸͓͒͐H̵͔͠È̶̖̳̘͍͓̂W̴̢̋̈͒͛̋I̶͕͑͠T̵̻͈̜͂̇Č̵̤̟̑̾̂̽H̸̰̺̏̓ ̴̜̗̝̱̹͛́̊̒͝⛥



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


    yea the full stack trace would help.


  • Registered Users Posts: 1,500 ✭✭✭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 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 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