建表如下:
CREATE TABLE `teacher` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`name` char(10) NOT NULL,
`address` varchar(50) DEFAULT '??',
`year` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=gbk;操作插入数据不成功 try
{
//装载驱动程序
Class.forName("com.mysql.jdbc.Driver").newInstance();
//创建连接
Connection con=DriverManager.getConnection(url);
//创建执行语句。
//===========================使用PreparedStatement========//
PreparedStatement pstmt=con.prepareStatement("insert into contact values(null,?,?,?)");
//=====================//
//pstmt.setString(1,null);
pstmt.setString(2,"aa");
pstmt.setString(3,"bb");
pstmt.setDate(4,new java.sql.Date(new java.util.Date().getTime())); pstmt.execute();
//===========================使用Statement========//
out.println("添加数据成功");
//关闭连接、释放资源
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
CREATE TABLE `teacher` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`name` char(10) NOT NULL,
`address` varchar(50) DEFAULT '??',
`year` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=gbk;操作插入数据不成功 try
{
//装载驱动程序
Class.forName("com.mysql.jdbc.Driver").newInstance();
//创建连接
Connection con=DriverManager.getConnection(url);
//创建执行语句。
//===========================使用PreparedStatement========//
PreparedStatement pstmt=con.prepareStatement("insert into contact values(null,?,?,?)");
//=====================//
//pstmt.setString(1,null);
pstmt.setString(2,"aa");
pstmt.setString(3,"bb");
pstmt.setDate(4,new java.sql.Date(new java.util.Date().getTime())); pstmt.execute();
//===========================使用Statement========//
out.println("添加数据成功");
//关闭连接、释放资源
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
你把这行注释了,后面的setString里的参数貌似应该就从1开始了吧