Solved

How can I put ResultSet with this existing code?

Posted on 2008-10-21
3
180 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
  • 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 500 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now