database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 123 456
    New Member
    • Mar 2012
    • 5

    #1

    database

    how can i fetch date type of data from oracle database?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Same way you get other types.
    Code:
    select columnName from tableName
    What problem are you getting exactly?

    Comment

    • 123 456
      New Member
      • Mar 2012
      • 5

      #3
      Actually i am entering date from keyboard and try to fetch it

      String sql= "select * from LogTable3 where logDate="+date;

      i am taking date as a user input, but its not working

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        What programming language are you using and what type is that date variable that you adding to the query?

        Comment

        • 123 456
          New Member
          • Mar 2012
          • 5

          #5
          i am using java and date variable i am taking as Date object

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            You need to brush up on your JDBC. You don't set date parameters like that. You set them on a PreparedStateme nt
            Code:
            String sql= "select * from LogTable3 where logDate=?";
            preparedStatement.setDate(1, date);

            Comment

            • 123 456
              New Member
              • Mar 2012
              • 5

              #7
              i am setting it by using statement obj bro...

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                I don't understand. You just said that you set it using

                String sql= "select * from LogTable3 where logDate="+date;

                above. That is not setting it using statement.

                Comment

                • 123 456
                  New Member
                  • Mar 2012
                  • 5

                  #9
                  String date= "25-jun-2012";
                  Statement stmt1= connection.crea teStatement();
                  String sql= "select * from LogTable3 where logDate="+date;
                  ResultSet rs1= stmt1.executeQu ery(sql);

                  now u understand or not?

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    1.) You said "date variable i am taking as Date object ". But you have
                    Code:
                    String date= "25-jun-2012";
                    That is a string not a date.
                    2.) Don't ever use Statement. Use PreparedStateme nt instead because
                    a.)PreparedStat ement will cache the compiled query on the database and so will be faster for multiple invocations
                    b.)PreparedStat ement protects against SQL injection.
                    3.) You should create a java.sql.Date object to set as the parameter in your prepared statement. You change the String to a Date using the SimpleDateForma t class, something like
                    Code:
                    date = new SimpleDateFormat("MM-dd-yyyy").parse("25-06-2012");
                    PreparedStatement ps = connection.prepareStatement(sql);	    ps.setDate(1, new java.sql.Date(date.getTime()));

                    Comment

                    Working...