简便使用Date类型:
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.PreparedStatement; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 8 9 public class test2 { 10 public static void main(String[] args) { 11 12 do_test2_all(); 13 } 14 15 private static int do_test2_all() 16 { 17 18 try { 19 20 Class.forName("");//空缺 21 String url = ""; //空缺 22 String userID = "";//空缺 23 String passwd = "";//空缺 24 Connection conn = DriverManager.getConnection(url, userID, passwd); 25 26 java.util.Date date = new java.util.Date(); 27 long t = date.getTime(); 28 java.sql.Date sqlDate = new java.sql.Date(t); 29 java.sql.Time sqlTime = new java.sql.Time(t); 30 31 String sql = ""; 32 PreparedStatement pstmt = null; 33 sql = "select * from student where date=? and id=? and name=?"; 34 pstmt = conn.prepareStatement(sql); 35 pstmt.setDate(1, sqlDate.valueOf("2019-01-01")); 36 pstmt.setInt(2, 1234); 37 pstmt.setString(3, "test"); 38 pstmt.executeUpdate(); 39 40 pstmt.close(); 41 conn.close(); 42 System.out.println("end"); 43 } 44 catch(ClassNotFoundException e){ 45 System.out.println(e.getMessage()); 46 } 47 catch(SQLException e){ 48 System.out.println(e.getMessage()); 49 } 50 51 return 0; 52 } 53 }
原文地址:https://www.cnblogs.com/yinguojin/p/10279089.html
时间: 2024-10-03 02:24:17