//to add, delete, change
Public int executeUpdate (String, SQL Object [] wenhao) {
Int wenhaoLength=0;
//access database connection success
If (this. GetDBConnection ()) {
Try {
//create a PreparedStatement
PSMT=connection. PrepareStatement (SQL);
//traverse Object [], filling a placeholder (?)
for(int i=0; i
}
WenhaoLength=PSMT. ExecuteUpdate ();
} the catch (SQLException e) {
e.printStackTrace();
}
}
Return wenhaoLength;
}
Interface:
//add news information
Public void addNews (int id, int categoryId, String title, String the summary, the String content, String the author, the Date createDate);
//modify specific news
Public void updateNews (String title, int id);
Implementation class:
//add news information
Public void addNews (int id, int categoryId, String title, String the summary, the String content, String the author, the Date createDate) {
Try {
String SQL="INSERT INTO news_detail (id, categoryId, title, summary, the content, the author, createDate) VALUE (? ,? ,? ,? ,? ,? ,?) ";
Object [] wenhao={id, categoryId, title, summary, the content, the author, the new Timestamp (createDate) getTime ())};
Int I=this. ExecuteUpdate (SQL, wenhao);
If (i> 0 {
System. The out. Println (" increase the news information ");
}
} the finally {
//release resources
Enclosing closeResult ();
}
}
//modify specific news
Public void updateNews (String title, int id) {
Try {
String SQL="UPDATE news_detail SET title=? WHERE id=?" ;
Object [] wenhao={} the title, id;
Int I=this. ExecuteUpdate (SQL, wenhao);
If (i> 0 {
System. The out. Println (" modify news information ");
}
} the finally {
//release resources
Enclosing closeResult ();
}
}
Delete can be normal execution, but why add and modify an error, Chou along while also didn't find what was wrong,,
This is the increase of running code: dao. AddNews (3, 2, "test", "test", "test", "zy", new Date ());
Error: com. Mysql. JDBC. MysqlDataTruncation: Data truncation: Incorrect datetime value: '3' for column 'createDate' at row 1
At com. Mysql. JDBC. MysqlIO. CheckErrorPacket (MysqlIO. Java: 2936)
At com. Mysql. JDBC. MysqlIO. SendCommand (MysqlIO. Java: 1601)
At com. Mysql. JDBC. MysqlIO. SqlQueryDirect (MysqlIO. Java: 1710)
At com. Mysql. JDBC. Connection. ExecSQL (2436) Connection. Java:
At com. Mysql. JDBC. PreparedStatement. ExecuteInternal (PreparedStatement. Java: 1402)
At com. Mysql. JDBC. PreparedStatement. ExecuteUpdate (PreparedStatement. Java: 1694)
At com. Mysql. JDBC. PreparedStatement. ExecuteUpdate (PreparedStatement. Java: 1608)
At com. Mysql. JDBC. PreparedStatement. ExecuteUpdate (PreparedStatement. Java: 1593)
The at cn. Kentucky. Dao. AllDao. ExecuteUpdate (42) AllDao. Java:
The at cn. Kentucky. Dao. Impl. NewsDaoimpl. AddNews (20) NewsDaoimpl. Java:
The at cn. Kentucky. Dao. Impl. NewsDaoimpl. Main (NewsDaoimpl. Java: 109)
Execute modified line console blank, what also don't show,
Run the code: dao updateNews (" newsTitle ", 3);
Bosses help me have a look, thanks
CodePudding user response:
111111CodePudding user response:
Bosses help me have a look, thank you very much!CodePudding user response:
Reading the wrong information is corresponding to the createDate field 3, PSMT. SetObject (I + 1, wenhao [0]). Wenhao [I]CodePudding user response: