Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: SQL Query

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default SQL Query

    I have Paradox database
    for connecting to db i use this class

    package sqlClasses;
     
    import java.sql.*;
     
    public class Paradox {
     
        public static Connection createConnection() {
            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
                Connection conn = DriverManager.getConnection("jdbc:odbc:Odds-Database", "user", "pass");
                return conn;
            } catch (Exception e) {
                System.out.println(e);
                System.exit(0);
                return null;
            }
        }
    }

    i want to fill table with data from database, and i use this method:

    public void roundTable() {
            TableModles tm = new TableModles();
            tm.roundTable();
            try {
                Connection con = Paradox.createConnection();
                Statement s = con.createStatement();
                s.executeQuery("SELECT Kolo, Od_datuma, Do_datuma FROM Kola");
                ResultSet rset = s.getResultSet();
                if (rset != null) {
                    tables.CreateTables.roundTable((DefaultTableModel) oddsmaker.OddsMakerView.roundTable.getModel(), rset);
                    con.close();
                }
            } catch (Exception e) {
                System.out.println(e);
            }
        }

    Its working fine but in the table where I put the columns "Od_datuma" and "Do_datuma" i need to fill table only with date , but with this code is filled with date and time.
    Db column is type DATE, how to edit the query to get only the DATE from db?


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: SQL Query

    in your executeQuery method you're selecting 3 columns from the table when u only need 1... change the statement to select whichever column holds the date information (im guessing "Od_datuma") and it should work

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SQL Query

    I need values from 3 columns from the table, "Kolo", "Od_datuma" and "Do_datuma", in the table columns Od_datuma and Do_datuma hold datetime values, I only need DATE values from this two columns

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: SQL Query

    a) Do this on the database side - I am not familiar with Paradox, but many databases have ways of converting one date data type to another (for instance, in MySQL you can use the DATE function). b) Do this after you pull the timestamp, use the Date object to format this however you wish.

  5. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SQL Query

    I have read on microsoft site that Paradox Data type can't be converted with operation cast or convert, so im looking for another solution

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: SQL Query

    Quote Originally Posted by mija View Post
    I have read on microsoft site that Paradox Data type can't be converted with operation cast or convert, so im looking for another solution
    Then use option b. Perform the query, use the getDate function of ResultSet(presuming the driver supports this operation), and use the returned Date object as needed.

  7. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SQL Query

    Quote Originally Posted by copeg View Post
    Then use option b. Perform the query, use the getDate function of ResultSet(presuming the driver supports this operation), and use the returned Date object as needed.
    ok, and how to fill JTable with that results, i need Kolo, Od_datuma, Do_datuma in one table row

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: SQL Query

    Quote Originally Posted by mija View Post
    ok, and how to fill JTable with that results, i need Kolo, Od_datuma, Do_datuma in one table row
    What did you try? Where are you stuck?

  9. #9
    Junior Member
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SQL Query

    Quote Originally Posted by copeg View Post
    What did you try? Where are you stuck?
    Im trying to get data from Paradox Database (table with columns Kolo, Od_datuma, Do_datuma) and i like to fill JTable with this values
    for ex. one row of JTable to contain data from one row of Paradox Table , with my code i do this but in JTable columns Od_datuma and Do_datuma are filling values with date and time, I like to fill this columns only with date I don't need time in this JTable

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: SQL Query

    Quote Originally Posted by mija View Post
    Im trying to get data from Paradox Database (table with columns Kolo, Od_datuma, Do_datuma) and i like to fill JTable with this values
    for ex. one row of JTable to contain data from one row of Paradox Table , with my code i do this but in JTable columns Od_datuma and Do_datuma are filling values with date and time, I like to fill this columns only with date I don't need time in this JTable
    Read post #8, which I will reiterate: what did you try?

  11. #11
    Junior Member
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SQL Query

    public void roundTable() {
            TableModles tm = new TableModles();
            tm.roundTable();
            try {
                Connection con = Paradox.createConnection();
                Statement s = con.createStatement();
                s.executeQuery("SELECT Kolo, Od_datuma, Do_datuma FROM Kola");
                ResultSet rset = s.getResultSet();
                if (rset != null) {
                    tables.CreateTables.roundTable((DefaultTableModel) oddsmaker.OddsMakerView.roundTable.getModel(), rset);
                    con.close();
                }
            } catch (Exception e) {
                System.out.println(e);
            }
        }

  12. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: SQL Query

    Sorry, I don't know what TableModles is, nor exactly where you are stuck. I recommend breaking the problem down as small as possible, being explicit about the problem, post an SSCCE, and ask a specific question regarding your code.

  13. #13
    Junior Member
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SQL Query

    TableModels is a class where I have Different Models of tables witch i use in my code, in this method i use Table Model with 3 columns and i fill table created with that table model with result set from query

  14. #14
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: SQL Query

    Quote Originally Posted by mija View Post
    TableModels is a class where I have Different Models of tables witch i use in my code, in this method i use Table Model with 3 columns and i fill table created with that table model with result set from query
    I feel like we are going in circles. Don't expect anyone to have any clue what you are talking about unless you provide all the details, better yet post code that is succinct and which clearly demonstrates what you are talking about (in other words, an SSCCE). I recommend reading what an SSCCE is, and reading the link in my signature entitled 'Getting Help'. There's not much more help I can provide at the moment.

Similar Threads

  1. What is sql:query var refers to and what's wrong with the below code ?
    By tangara in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: October 8th, 2011, 05:24 AM
  2. UPDATE SQL QUERY by getting user variables
    By bondage in forum JDBC & Databases
    Replies: 5
    Last Post: April 22nd, 2011, 03:44 PM
  3. update query is firing first then insert query
    By salmondavid88 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 8th, 2011, 10:15 AM
  4. SQL query not working
    By mjpam in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 28th, 2010, 05:15 PM
  5. Access database SQL query help
    By mjpam in forum JDBC & Databases
    Replies: 3
    Last Post: September 8th, 2010, 08:48 AM