码迷,mamicode.com
首页 > 数据库 > 详细

Java获取Date类型-针对SQL语句

时间:2019-01-16 20:44:38      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:etc   catch   turn   mes   print   使用   main   util   statement   

 

简便使用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 }

 

Java获取Date类型-针对SQL语句

标签:etc   catch   turn   mes   print   使用   main   util   statement   

原文地址:https://www.cnblogs.com/yinguojin/p/10279089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!