当我按下按钮时,我使用此代码,它应该将值插入数据库
code else if(a.getSource()==New){
try{
if(Cpphone.getText().equals("")||Dnname.getText().equals("")||array.equals("")|| totalcost==0)
{
JOptionPane.showMessageDialog(this,
"Please insert all information before submitting.", "", JOptionPane.INFORMATION_MESSAGE);
}
else{
Connection mycon= DriverManager.getConnection("jdbc:mysql://localhost:3306/projectofpharmacy","root","sherouk");
Statement mystat=mycon.createStatement();
mystat.executeUpdate("insert into pharmacy_orders (patintphone,doctorName,OrderdateTime,totalCost) values ('"+Cpphone.getText()+"','"+Dnname.getText()+"',now(),'"+totalcost+"'");
JOptionPane.showMessageDialog(this,
"the process is done successfuly.", "", JOptionPane.INFORMATION_MESSAGE);
array=null;
totalcost=0;
}
}
catch(Exception e){
System.out.println(e);
JOptionPane.showMessageDialog(this,
e.toString(), "", JOptionPane.INFORMATION_MESSAGE);
}
}
I get this error (java.sql.SQLSyntaxErrorException: 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 '' at line 1)
最佳答案
我认为问题出在 now() 方法中。 尝试这样的事情:
private static java.sql.Date getCurrentDate() {
java.util.Date today = new java.util.Date();
return new java.sql.Date(today.getTime());
}
String insertTableSQL = "INSERT INTO DBUSER"
+ "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES"
+ "(?,?,?,?)";
preparedStatement = dbConnection.prepareStatement(insertTableSQL);
preparedStatement.setDate(4, getCurrentDate());
关于java - 从 jtextfield java GUI 将值插入到 mysql 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54539112/