使用mysql建立一个表,字段如下
create table user
(
userId int not null,
userName varchar(20),
address varchar(100),
phone char(10),
startTime date,
finishTime datetime,
primary key (userId)
);
下面是运行到jdbc代码,仍然有问题-----时间无法插入
package com.sun.test;import java.sql.*;import java.util.Date;
public class TestConMain { public static void main(String[] args) {
//假设这两个参数就是从jsp传过来到字符串
String date1="";
String date2="";
Connection conn = null;
PreparedStatement pre=null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/lv?user=root&password=root");
//插入一条语句,可以运行,说明正常
//String sql="insert into user values(12,?,null,null,null,null)";
//pre.setString(1,"wattor");
//插入一条带有时间的语句,出现问题
String sql="insert into user values(13,null,null,null,?,null)";
pre=conn.prepareStatement(sql);
pre.setDate(1,new Date()); //这里希望能插入上边字符串变量---例如前台jsp传来的 pre.execute(); } catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
} if (stmt != null) {
stmt.close();
stmt = null;
} if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
} }}
create table user
(
userId int not null,
userName varchar(20),
address varchar(100),
phone char(10),
startTime date,
finishTime datetime,
primary key (userId)
);
下面是运行到jdbc代码,仍然有问题-----时间无法插入
package com.sun.test;import java.sql.*;import java.util.Date;
public class TestConMain { public static void main(String[] args) {
//假设这两个参数就是从jsp传过来到字符串
String date1="";
String date2="";
Connection conn = null;
PreparedStatement pre=null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/lv?user=root&password=root");
//插入一条语句,可以运行,说明正常
//String sql="insert into user values(12,?,null,null,null,null)";
//pre.setString(1,"wattor");
//插入一条带有时间的语句,出现问题
String sql="insert into user values(13,null,null,null,?,null)";
pre=conn.prepareStatement(sql);
pre.setDate(1,new Date()); //这里希望能插入上边字符串变量---例如前台jsp传来的 pre.execute(); } catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
} if (stmt != null) {
stmt.close();
stmt = null;
} if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
} }}
解决方案 »
- 如何定义一个 Stack<String> 数组
- nodeName為#text的節點是什麽東西?
- java.util.Date转换为java.sql.Date的问题
- java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]无效的描述符索引
- removeAll()之后再次添加jpanel组件,为什么一直不现实jpanel组件呀!
- 前(钱)途问题,来者有分。
- 我想把0A赋给char类型的变量,请问怎么写啊?
- If问题
- Java给我的麻烦!(有分!!)
- JFrame如何实现单击一个按钮出现另一个窗口
- 电脑同时插入50个优盘,如何把数据写入50个优盘里面?
- JFreeChart传值乱码的问题
---这里用String date1=""; String date2=""; 作为从jsp传来的数据jsp传来到字符格式为 2008-10-25 18:30:25 请问怎么输入到数据库中(数据库表到格式已经确定,不能修改,只能改java代码)
---这里用String date1=""; String date2=""; 作为从jsp传来的数据jsp传来到字符格式为 2008-10-25 18:30:25 请问怎么输入到数据库中(数据库表到格式已经确定,不能修改,只能改java代码)
改成
pre.setObject(1,new java.sql.Timestamp(...))
这里的Date必须是java.sql.Date不是java.util.Date