[問題] 想用java抓Date 再存入MySQL

看板java作者 (慢慢的看清楚了...)時間16年前 (2009/07/30 21:32), 編輯推噓6(603)
留言9則, 6人參與, 最新討論串1/1
我希望能在抓資料進DB的同時,也能一併記下時間 目前遇到的問題是 MySQL吃的Date格式是 YYYY-MM-DD 但我弄出的時間 往往會變成這種格式 'Jul 30 21:30:13 CST 2009)' 也許各位大大有更好的方法 請不吝賜教 <(_ _)> ============================================================================= main方法中 String driver = "com.mysql.jdbc.Driver"; String urlString = //帳密跳過 "jdbc:mysql://j.snpy.org/java33?user=?&password=?&+ useUnicode=true&characterEncoding=UTF-8"; Connection conn = null; Statement smt = null; DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");// try { Class.forName(driver); conn = DriverManager.getConnection(urlString); smt = conn.createStatement(); String sql = "Insert Into `threeindex` (`date`) +Values(new java.util.Date())"; // 重點 ^^^^^^^^^^^^^^^^^^^^ smt.execute(sql); conn.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } ============================================================================= 錯誤訊息 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Jul 30 21:30:13 CST 2009)' at line 1 ^^^^^^^^^^^^^^^^^^^^^^^^^^ at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.Util.getInstance(Util.java:381) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2548) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2477) at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:741) at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:587) at Threeindex03.main(Threeindex03.java:112) ============================================================================== -- 自古多情空餘恨 從來殘忍少添傷~ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.124.98.199

07/30 22:11, , 1F
SimpleDateFormat
07/30 22:11, 1F

07/30 22:57, , 2F
為什麼不直接用 MySQL 的 NOW() 函式
07/30 22:57, 2F

07/31 01:27, , 3F
你的 sql 句子一整個很奇怪阿... 是你貼錯嗎?
07/31 01:27, 3F

07/31 01:30, , 4F
btw... 我直接用 new Date().toString() 可以塞....
07/31 01:30, 4F

07/31 12:47, , 5F
感謝! String sql = "Insert Into `threeindex` (`date`)
07/31 12:47, 5F

07/31 12:48, , 6F
+Values((select NOW()))"; 就搞定了! 但這是靠MySQL語法 XD
07/31 12:48, 6F

07/31 15:41, , 7F
請 bind parameter 吧, 不要直接建 SQL...
07/31 15:41, 7F

08/02 19:03, , 8F
靠特定資料庫的語法也沒什麼不好,輕易地轉換DB只是個夢。
08/02 19:03, 8F

08/04 00:01, , 9F
如果支援多個DB的時候,每個資料庫的語法都要寫一遍嗎Orz
08/04 00:01, 9F
文章代碼(AID): #1ASQ3gwM (java)