如何使用JDBC API將字串轉換為日期物件?


Date物件的valueOf()方法接受一個字串值,表示JDBC轉義格式中的一個日期,即yyyy-mm-dd,並將給定的字串值轉換為java.sql.Date物件。

Date date = Date.valueOf(“date_string”);

假設我們建立了一個名為employee_data的表,描述如下

+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | int(11)      | YES  |     | NULL    |       |
| Name     | varchar(255) | YES  |     | NULL    |       |
| Dob      | date         | YES  |     | NULL    |       |
| Location | varchar(255) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+

下面的JDBC程式接受員工的id(整數)、name(字串)、出生日期(字串)和location(字串),將以JDBC轉義語法格式傳遞的出生日期值轉換為Date物件,並將給定的詳細資訊插入到employee_data表中。最後,一次檢索表中的所有記錄並顯示。

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class StringtoDate {
   public static void main(String args[])throws Exception {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql:///mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating a Statement object
      Statement stmt = con.createStatement();
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the number of records you need to insert: ");
      int num = sc.nextInt();
      //Inserting values to the table
      String query = "INSERT INTO employee_data VALUES (?, ?, ?, ?)";
      PreparedStatement pstmt = con.prepareStatement(query);
      for(int i=1; i<=num; i++) {
         System.out.println("Enter the Employee ID: ");
         int id = sc.nextInt();
         System.out.println("Enter the Employee name: ");
         String name =sc.next();
         System.out.println("Enter the Employee DOB in the format yyyy-mm-dd : ");
         String dateOfBirth = sc.next();
         System.out.println("Enter the Employee Location : ");
         String loc = sc.next();
         pstmt.setInt(1,id);
         pstmt.setString(2, name );
         pstmt.setDate(3, Date.valueOf(dateOfBirth));
         pstmt.setString(4, loc);
         pstmt.executeUpdate();
      }
      System.out.println("data inserted");
      //Creating Statement object
      stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select * from employee_data");
      //Retrieving values
      while(rs.next()) {
         System.out.println("Employee_Id: "+rs.getInt("ID"));
         System.out.println("Employee_Name: "+rs.getString("Name"));
         System.out.println("Employee_DOB: "+rs.getInt("DOB"));
         System.out.println("Employee_Location: "+rs.getString("Location"));
         System.out.println();
      }
   }
}

輸出

Connection established......
table created......
Enter the number of records you need to insert in the table:
3
Enter the Employee ID:
1001
Enter the Employee name:
Krishna
Enter the Employee DOB in the format yyyy-mm-dd :
1989-09-26
Enter the Employee Location :
Hyderabad
Enter the Employee ID:
1002
Enter the Employee name:
Kasyap
Enter the Employee DOB in the format yyyy-mm-dd :
1990-06-25
Enter the Employee Location :
Vishakhapatnam
Enter the Employee ID:
1003
Enter the Employee name:
Maruthi
Enter the Employee DOB in the format yyyy-mm-dd :
1995-06-06
Enter the Employee Location :
Vijayawada
data inserted
Employee_Id: 1001
Employee_Name: Krishna
Employee_DOB: 1989
Employee_Location: Hyderabad

Employee_Id: 1002
Employee_Name: Kasyap
Employee_DOB: 1990
Employee_Location: Vishakhapatnam

Employee_Id: 1003
Employee_Name: Maruthi
Employee_DOB: 1995
Employee_Location: Vijayawada

更新於: 30-Jul-2019

1K+瀏覽

開啟你的 職業

透過完成課程獲取認證

開始
廣告
© . All rights reserved.