日期Java到MySql DateTime [英] Date Java to MySql DateTime
本文介绍了日期Java到MySql DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您的SQL语法有错误;检查手册,
对应于您的MySQL服务器版本,正确的
语法在'14:37:41)'附近使用第1行
的代码
public String addName(){
// TODO自动生成的方法stub
try {
java.util.Date dt = new java.util.Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
yyyy-MM-dd HH:mm:ss);
String currentTime = sdf.format(dt);
String name =RandomName;
连接connect = DriverManager.getConnection(
jdbc:mysql:// localhost,ericman,ericman);
语句stat =(Statement)connect.createStatement();
String insert =INSERT INTO`bookcatalog`.`puch`(`name`,`time`)VALUES('
+ name +',+ currentTime +);
stat.executeUpdate(insert);
} catch(Exception e){
System.out.println(e);
}
返回更新名称;
}
任何建议为什么会发生这种情况,我吮吸结构化语言,所以你知道:
解决方案
使用 PreparedStatement
。
String insert =INSERT INTO`bookcatalog`.`puch`(`name`,`time`)VALUES(?,?);
PreparedStatement ps = connect.prepareStatement(insert);
ps.setString(1,name);
ps.setTimeStamp(2,TimeStamp.valueOf(currentTime));
ps.executeUpdate();
every body. I am getting this error:
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 '14:37:41)' at line 1
for this piece of code
public String addName() {
// TODO Auto-generated method stub
try {
java.util.Date dt = new java.util.Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(dt);
String name = "RandomName";
Connection connect = DriverManager.getConnection(
"jdbc:mysql://localhost", "ericman", "ericman");
Statement stat = (Statement) connect.createStatement();
String insert = "INSERT INTO `bookcatalog`.`puch` (`name`, `time`) VALUES ('"
+ name + "', " + currentTime + ")";
stat.executeUpdate(insert);
} catch (Exception e) {
System.out.println(e);
}
return "Name Updated";
}
Any suggestion of why this happening, I suck on structured language just so you know :)
解决方案
Use PreparedStatement
.
String insert = "INSERT INTO `bookcatalog`.`puch` (`name`, `time`) VALUES (?,?)";
PreparedStatement ps=connect.prepareStatement(insert);
ps.setString(1,name);
ps.setTimeStamp(2,TimeStamp.valueOf(currentTime));
ps.executeUpdate();
这篇关于日期Java到MySql DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文