我試圖連接到mySQL DB,並在我的Java應用程序的3列osName,version,notes中執行Insert。教程im以下是http://alvinalexander.com/java/java-mysql-insert-example-preparedstatement。任何輸入或評論都很有幫助。這是我的數據庫信息 -MySQL Java數據插入錯誤
CREATE TABLE oslog
。 entry
( osName
VARCHAR(10)NOT NULL, version
VARCHAR(10)NOT NULL, notes
VARCHAR(20)NOT NULL, PRIMARY KEY(osName
) )ENGINE = InnoDB的;
package com.tutorialspoint.struts2;
import java.sql.*;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport{
private String osName;
private String version;
private String notes;
public String execute() throws Exception {
return "success";
}
{
try
{
// created a datbase connection
String myDriver = "org.gjt.mm.mysql.Driver";
String myURL = "jdbc:mysql://localhost/HelloWorld";
Class.forName(myDriver);
Connection conn = DriverManager.getConnection(myURL, "root", "");
//insert statement
String query = "INSERT INTO oslog.entry (osName,version,notes " +
"values osName, version, notes";
//mysql insert preparedstatement
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setString(1, osName);
preparedStmt.setString(2, version);
preparedStmt.setString(3, notes);
//excute the preparedstatement
preparedStmt.execute();
conn.close();
}
catch(Exception e)
{
System.err.println("Got an Exception!");
System.err.println(e.getMessage());
}
}
public String getOsName() {
return osName;
}
public void setOsName(String osName) {
this.osName = osName;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public void validate()
{
if (osName == null || osName.trim().equals(""))
{
addFieldError("osName","The OS name is required");
}
if (version == null || version.trim().equals(""))
{
addFieldError("version","The OS version is required");
}
}
}
我相信你需要圍繞你的查詢值的圓括號;你還沒有關閉第一輪支架(圍繞欄目名稱)。 – adamdc78 2015-01-21 01:22:50
什麼是錯誤? – 2015-01-21 01:22:56
不返回錯誤。無論發生什麼,我都有我的jsp和行動取得成功。 – Kyas 2015-01-21 02:43:19