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();
}
}
}
ASKER
I need to update records with the date not just println
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window