我正在使用JDBC oracle数据库在Netbeans IDE中进行java项目。我需要在数据库中插入日期和时间,格式为 11/16/2013 10:30 < / strong>即可。 我在下面附上了我的代码。
在 DBConnector
中插入代码public void insert(String id, String uid, String vmid, String ttid, String faid, String taid, String fdate, String tdate, String onbooking, String msbooking, String bcreated)
throws SQLException {
Statement stmt = null;
String query = "Insert into DMUSER.CAB2("
+ "ID,"
+ "USER_ID,"
+ "VEHICLE_MODEL_ID,"
+ "TRAVEL_TYPE_ID,"
+ "FROM_AREA_ID,"
+ "TO_AREA_ID,"
+ "FROM_DATE,"
+ "TO_DATE,"
+ "ONLINE_BOOKING,"
+ "MOBILE_SITE_BOOKING,"
+ "BOOKING_CREATED"
+ ") values ("
+ id + ","
+ uid + ","
+ vmid + ","
+ ttid + ","
+ faid + ","
+ taid + ","
+ fdate + ","
+ tdate + ","
+ onbooking + ","
+ msbooking + ","
+ bcreated + ""
+ ")";
按钮点击活动
private void btn_insertActionPerformed(java.awt.event.ActionEvent evt) {
txt_id.setText("132536");
txt_user_id.setText("16");
txt_vechile_mdl_id.setText("28");
txt_from_area_id.setText("58");
txt_to_area_id.setText("1063");
txt_frm_date.setText("11/16/2013 9:30");
txt_to_date.setText("NULL");
txt_booking_created.setText("11/16/2013 9:30");
txt_travel_type_id.setText("2");
txt_mobile_site_booking.setText("0");
txt_online_booking.setText("1");
}
private void btn_predictionActionPerformed(java.awt.event.ActionEvent evt) {
try {
String id = txt_id.getText();
String uid = txt_user_id.getText();
String vmid = txt_vechile_mdl_id.getText();
String ttid = txt_travel_type_id.getText();
String faid = txt_from_area_id.getText();
String fdate = txt_frm_date.getText();
String tdate = txt_to_date.getText();
String onbooking = txt_online_booking.getText();
String msbooking = txt_mobile_site_booking.getText();
String bcreated = txt_booking_created.getText();
String taid = txt_to_area_id.getText();
DB_Connector con = new DB_Connector();
try {
con.insert(id, uid, vmid, ttid, faid, taid, fdate, tdate, onbooking, msbooking, bcreated);
} catch (SQLException ex) {
Logger.getLogger(CabCancellationUI.class.getName())。log(Level.SEVERE,null,ex); } } catch(Exception ex){ } }
这里的问题是,在数据库中,日期列是VARCHAR
数据类型,所以如何将此 11/16/2013 10:30 插入数据库。请帮我把它排除在外。
已编辑:发生异常
QUERY ::插入 DMUSER.CAB2(ID,USER_ID,VEHICLE_MODEL_ID,TRAVEL_TYPE_ID,FROM_AREA_ID,TO_AREA_ID,FROM_DATE,TO_DATE,ONLINE_BOOKING,MOBILE_SITE_BOOKING,BOOKING_CREATED) 值(132536,16,28,2,58,1063,11 / 16/2013 10:30,NULL,1,0,01) java.sql.SQLSyntaxErrorException:ORA-00917:缺少逗号
在 oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:91) 在 oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133) 在 oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:206) 在oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:455)at at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:413)at at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:1034)at at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:183)at at oracle.jdbc.driver.T4CStatement.executeForRows(T4CStatement.java:942) 在 oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1222) 在 oracle.jdbc.driver.OracleStatement.executeUpdateInternal(OracleStatement.java:1706) 在 oracle.jdbc.driver.OracleStatement.executeUpdate(OracleStatement.java:1674) 在 oracle.jdbc.driver.OracleStatementWrapper.executeUpdate(OracleStatementWrapper.java:275) 在Database.DB_Connector.insert(DB_Connector.java:78)at UI.CabCancellationUI.btn_predictionActionPerformed(CabCancellationUI.java:365) 在UI.CabCancellationUI.access $ 500(CabCancellationUI.java:28)at UI.CabCancellationUI $ 6.actionPerformed(CabCancellationUI.java:151)at at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) 在 javax.swing.AbstractButton中的$ Handler.actionPerformed(AbstractButton.java:2348) 在 javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 在 javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 在 javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) 在java.awt.Component.processMouseEvent(Component.java:6535)at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)at java.awt.Component.processEvent(Component.java:6300)at java.awt.Container.processEvent(Container.java:2236)at java.awt.Component.dispatchEventImpl(Component.java:4891)at java.awt.Container.dispatchEventImpl(Container.java:2294)at java.awt.Component.dispatchEvent(Component.java:4713)at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) 在 java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) 在java.awt.Container.dispatchEventImpl(Container.java:2280)at java.awt.Window.dispatchEventImpl(Window.java:2750)at java.awt.Component.dispatchEvent(Component.java:4713)at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)at at java.awt.EventQueue.access $ 500(EventQueue.java:97)at java.awt.EventQueue $ 3.run(EventQueue.java:709)at java.awt.EventQueue $ 3.run(EventQueue.java:703)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) 在 java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) 在java.awt.EventQueue $ 4.run(EventQueue.java:731)at java.awt.EventQueue $ 4.run(EventQueue.java:729)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) 在java.awt.EventQueue.dispatchEvent(EventQueue.java:728)at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) 在 java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) 在java.awt.EventDispatchThread.run(EventDispatchThread.java
答案 0 :(得分:3)
这个问题不是你想的。例外是抱怨用于插入数据的错误的sql语法。
您需要在insert方法中更正查询字符串。此外,您已接受为字符串的每个表列,这似乎不正确。考虑表的每一列的正确数据类型,例如ID应该是数字,而不是varchar等。
如果每个参数都是string类型,那么正确的代码必须在查询字符串中的参数之前和之后的任何地方添加'
,如下所示:
String query = "Insert into DMUSER.CAB2("
+ "ID,"
+ "USER_ID,"
+ "VEHICLE_MODEL_ID,"
+ "TRAVEL_TYPE_ID,"
+ "FROM_AREA_ID,"
+ "TO_AREA_ID,"
+ "FROM_DATE,"
+ "TO_DATE,"
+ "ONLINE_BOOKING,"
+ "MOBILE_SITE_BOOKING,"
+ "BOOKING_CREATED"
+ ") values ("
+ "'"+ id + "',"
+ "'" + uid + "',"
+ "'" + vmid + "',"
+ "'" + ttid + "',"
+ "'" + faid + "',"
+ "'" + taid + "',"
+ "'" + fdate + "',"
+ "'" + tdate + "',"
+ "'" + onbooking + "',"
+ "'"+ msbooking + "',"
+ "'"+bcreated + "'"
+ ")";
最后,这是一个糟糕的代码。您应该考虑使用PreparedStatement。