Link to home
Start Free TrialLog in
Avatar of glynco
glynco

asked on

How can I put ResultSet with this existing code?

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

Avatar of chaitu chaitu
chaitu chaitu
Flag of India image


 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

Avatar of glynco
glynco

ASKER

I need to update records with the date not just println
ASKER CERTIFIED SOLUTION
Avatar of chaitu chaitu
chaitu chaitu
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial