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");
}
}
}