1

Im trying to connect to mySQL DB and perform an Insert on the 3 columns osName,version,notes in my Java application. The tutorial im following is http://alvinalexander.com/java/java-mysql-insert-example-preparedstatement. Any input or comments are helpful. Here is my Database info -

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");
    }
}
}
3
  • I believe you need round brackets around your values in the query; you've also not closed the first round bracket (around column names). Commented Jan 21, 2015 at 1:22
  • Does not return an error. I have my jsp and actions to be successful no matter what happens. Commented Jan 21, 2015 at 2:43
  • 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 'values null, null, null' at line 1 Commented Jan 21, 2015 at 8:12

2 Answers 2

2

I see you are using PreparedStatement You need to change your query String from:

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

to

 String query = "INSERT INTO oslog.entry(osName,version,notes) " + 
        "values (?, ?, ?)";
Sign up to request clarification or add additional context in comments.

1 Comment

String query = "INSERT INTO oslog.entry(osName,version,notes) " + "values (?, ?, ?)"; That didn't fix it. I dont have any errors coming up.
2

You are using PreparedStatement, so your query should have placeholders. Try this:

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

7 Comments

"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 'values null, null, null' at line 1" is the error i get.
@Kyas That is because you never initialize the variables that you pass to preparedStmt (osName,version, notes) and your table doesn't accept null for those columns.
I can change my table but How would i initialize the variables?
@Kyas you have not used the string i mentioned in my answer, you seems to be missing '(' after values. I believe your sql string is like "values ?, ?, ?"; instead of "values (?, ?, ?)";
I have changed the action file as such: String query = "INSERT INTO oslog.entry (osName,version,notes) " + "values (?, ?, ?)";
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.