2015-01-21 83 views
1

我試圖連接到mySQL DB,並在我的Java應用程序的3列osName,version,notes中執行Insert。教程im以下是http://alvinalexander.com/java/java-mysql-insert-example-preparedstatement。任何輸入或評論都很有幫助。這是我的數據庫信息 -MySQL Java數據插入錯誤

CREATE TABLE oslogentryosName 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"); 
    } 
} 
} 
+0

我相信你需要圍繞你的查詢值的圓括號;你還沒有關閉第一輪支架(圍繞欄目名稱)。 – adamdc78 2015-01-21 01:22:50

+0

什麼是錯誤? – 2015-01-21 01:22:56

+0

不返回錯誤。無論發生什麼,我都有我的jsp和行動取得成功。 – Kyas 2015-01-21 02:43:19

回答

2

我看你使用PreparedStatement 您需要從改變你的query字符串:

String query = "INSERT INTO oslog.entry (osName,version,notes " + 
     "values osName, version, notes"; 

String query = "INSERT INTO oslog.entry(osName,version,notes) " + 
     "values (?, ?, ?)"; 
+0

String query =「INSERT INTO oslog.entry(osName,version,notes)」+ 「values (?,?,?)「;這並沒有解決它。我沒有任何錯誤出現。 – Kyas 2015-01-21 03:26:40

2

您使用PreparedStatement,所以您的查詢應該有佔位符。試試這個:

String query = "INSERT INTO oslog.entry (osName,version,notes) " + 
      "values (?, ?, ?)"; 
+0

「你的SQL語法錯誤;檢查對應於你的MySQL服務器版本的手冊,在第一行使用'values null,null,null'附近的正確語法」是我得到的錯誤。 – Kyas 2015-01-21 08:12:38

+0

@Kyas這是因爲你永遠不會初始化你傳遞給'preparedStmt'('osName','version','notes')的變量,而你的表不接受'null'這些列。 – Titus 2015-01-21 10:30:03

+0

我可以更改我的表,但我將如何初始化變量? – Kyas 2015-01-21 18:58:26