如何使用 JDBC API 将字符串转换为 Date 对象?
jdbcjava 8object oriented programmingprogramming
Date 对象的 valueOf() 方法接受表示 JDBC 转义格式(即 yyyy-mm-dd)的 Date 的字符串值,并将给定的字符串值转换为 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(整数)、姓名(字符串)、出生日期(字符串)和位置(字符串),将以 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 { //注册驱动程序 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //获取连接 String mysqlUrl = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //创建 Statement 对象 Statement stmt = con.createStatement(); Scanner sc = new Scanner(System.in); System.out.println("输入需要插入的记录数:"); int num = sc.nextInt(); //将值插入表中 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"); //创建 Statement 对象 stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from employee_data"); //检索值 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