Results 1 to 8 of 8
  1. #1
    Sanjaya is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Java Database handling problem

    Connection con;
    Statement stmt;
    ResultSet rs;

    try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");


    String dataSourceName = "Database";
    String dbURL = "jdbc:odbc:" + dataSourceName;
    Connection con = DriverManager.getConnection(dbURL, "","");


    stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIV E,
    ResultSet.CONCUR_READ_ONLY);
    String sql="SELECT DISTINCT DaysGiven FROM IssueBooks";
    rs=stmt.executeQuery(sql);


    DefaultListModel listModel = new DefaultListModel();

    while(rs.next()){
    listModel.addElement(rs.getString("DaysGiven"));
    }

    list1.setModel(listModel);
    stmt.close();
    }

    catch(Exception err){
    System.out.println(err.getMessage());
    }
    }

    Please tell me what the wrong with this code is. When I change the sql command like this "SELECT DaysGiven FROM IssueBooks" it works. But I want to use DISTINCT keyword to remove duplicates. Please tell me how to do that.
    Last edited by Sanjaya; 12-03-2012 at 11:48 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Java Database handling problem

    Please use [code] tags [/code] when posting code.
    Unformatted code is hard to read.

    What exception or error do you get when running the code with the DISTINCT keyword?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Java Database handling problem

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Sanjaya is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: Java Database handling problem

    'Invalid cursor position' error is given when running with DISTINCT keyword

  5. #5
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Java Database handling problem

    Which database are you using?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Java Database handling problem

    Don't use SCROLL_SENSITIVE.
    Why people insist on using scrollable result sets I do not know.
    They're costly, especially the sensitive ones.

    You do not need it.
    Just use the createStatement() with no parameters.

    I don't know if that is the problem, but I'm guessing you're on something like Access.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Sanjaya is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: Java Database handling problem

    Database is Ms Access 2007

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Java Database handling problem

    And what happens if you remove all the SCROLL stuff from your createStatement() call, like i suggested?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. java file handling problem
    By decode_urself in forum New To Java
    Replies: 5
    Last Post: 04-05-2012, 02:35 PM
  2. java insert data into database problem
    By chyeeqi in forum NetBeans
    Replies: 3
    Last Post: 03-29-2010, 01:36 AM
  3. Java database/method display problem
    By thelinuxguy in forum Advanced Java
    Replies: 3
    Last Post: 02-20-2009, 07:56 PM
  4. Problem in File Handling in Java
    By getbiplab in forum New To Java
    Replies: 6
    Last Post: 03-31-2008, 12:02 PM
  5. Problem connecting to my Database using Java
    By javaneed in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 09:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •