数据库问题,用MYSQL往表中插入数据出错(含错误代码)

liang__ liang__ 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
下面这个是我的表。用的是MySql数据库
create table booksInfo(bookid varchar(20), bookname varchar(30),bookauthor varchar(20),num int,publisher varchar(50) , begintime datetime);

而我在做往表中插入数据时出现错误,

错误代码:
 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 '33'asdfasdf''1987-1-1')' at line 1com.mysql.jdbc.exceptions.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 '33'asdfasdf''1987-1-1')' at line 1

部分JAVA代码:

public void button2_actionPerformed(ActionEvent e){
int sl;
String bianhao = jtbianhao.getText().trim();
String zuozhe = jtzuozhe.getText().trim();
String shuliang = jtshuliang.getText().trim();
String shuming = jtshuming.getText().trim();
String chubanshe = jtchubanshe.getText().trim();
String ruku = jtdate.getText().trim();
if(bianhao.equals(""))
JOptionPane.showMessageDialog(null, "图书编号不能为空,请继续填写```", "信息填写错误",
JOptionPane.ERROR_MESSAGE);
else if (zuozhe.equals(""))
JOptionPane.showMessageDialog(null, "图书作者不能为空,请继续填写```", "信息填写错误",
JOptionPane.ERROR_MESSAGE);
else if(shuliang.equals(""))
JOptionPane.showMessageDialog(null, "图书数量不能为空,请继续填写```", "信息填写错误",
JOptionPane.ERROR_MESSAGE);
else if(shuming.equals(""))
JOptionPane.showMessageDialog(null, "图书名称不能为空,请继续填写```", "信息填写错误",
JOptionPane.ERROR_MESSAGE);
else if(chubanshe.equals(""))
JOptionPane.showMessageDialog(null, "出版社不能为空,请继续填写```", "信息填写错误",
JOptionPane.ERROR_MESSAGE);
else if(ruku.equals(""))
JOptionPane.showMessageDialog(null, "入库时间不能为空,请继续填写```", "信息填写错误",
JOptionPane.ERROR_MESSAGE);
else{
try {
sl = Integer.parseInt(shuliang);
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
JOptionPane.showMessageDialog(null, "图书的 数量 只能是数字!!!,请继续填写```",
"数量填写错误",
JOptionPane.ERROR_MESSAGE);
return;
}
String sql = "insert into booksinfo (bookid,bookname,bookauthor,num,publisher,begintime) values(??????)";
PreparedStatement pe=null;
ResultSet rs=null;
try {
pe = db.conn.prepareStatement(sql);
pe.setString(1, bianhao);
pe.setString(2, shuming);
pe.setString(3, zuozhe);
pe.setInt(4, sl);
pe.setString(5, chubanshe);
pe.setString(6, ruku);
if(db.add(pe)){
db.myClose(1);
JOptionPane.showMessageDialog(null, "增加成功");
}
else
JOptionPane.showMessageDialog(null, "添加失败");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

}

public boolean add(PreparedStatement pt)
{

int result=-1;
try {
result = pt.executeUpdate();

} catch (SQLException ex) {
System.out.println(ex.toString());
ex.printStackTrace();
}
if(result>0)
return true;
else
return false;
}
...全文

相关推荐

评论 11
收藏 已收藏
分享
返回顶部
liang__
Java SE 62,621 成员 307,251 内容