Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I put ResultSet with this existing code?

Posted on 2008-10-21
3
Medium Priority
?
191 Views
Last Modified: 2012-05-05
How can I enter ResultSets while loop so that all of the records will be given the Date value.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
 
public class DemoPreparedStatementSetDate {
  public static java.sql.Date getCurrentJavaSqlDate() {
    java.util.Date today = new java.util.Date();
    return new java.sql.Date(today.getTime());
  }
 
  public static Connection getConnection() throws Exception {
 
    String url = "jdbc:mysql://localhost/datedb";
    String username = "root";
    String password = "root";
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
 
  public static void main(String[] args) throws Exception {
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
      conn = getConnection();
      String query = "insert into datetable(id, date_column) values(?, ?)";
      pstmt = conn.prepareStatement(query);
      pstmt.setString(1, "0001");
      java.sql.Date date = getCurrentJavaSqlDate();
      pstmt.setDate(2, date);
 
      // execute query, and return number of rows created
      int rowCount = pstmt.executeUpdate();
      System.out.println("rowCount=" + rowCount);
    } finally {
      pstmt.close();
      conn.close();
    }
  }
}

Open in new window

0
Comment
Question by:glynco
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 22773085

 public static void main(String[] args) throws Exception {
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
      conn = getConnection();
      String query = "select * from datetable";
      pstmt = conn.prepareStatement(query);
  
      ResultSet rs= pstmt.executeqQuery();
      while(rs.next()
{
 system.out.println(rs.getString(1));
 system.out.println(rs.getDate(2));
}
    } finally {
      pstmt.close();
      conn.close();
    }
  }

Open in new window

0
 

Author Comment

by:glynco
ID: 22773378
I need to update records with the date not just println
0
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 2000 total points
ID: 22773565

 String query = "update datetable set date_column=? where id =?";
      pstmt = conn.prepareStatement(query);
       java.sql.Date date = getCurrentJavaSqlDate();
 
      pstmt.setDate(1, date);
      pstmt.setString(2, "0001");
 
      // execute query, and return number of rows created
      int rowCount = pstmt.executeUpdate();

Open in new window

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question