如何使用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://127.0.0.1/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
广告