I am trying to use a Derby embedded database. I added the jar file and library and was able to create the tables. However, for some reason, when I try to insert data, an exception is triggered. It reads:
"java.sql.SQLSyntaxErrorException: Syntax error: Encountered
"testName" at line 1, column 18."
I am absolutely sure that I am providing the correct table and column name. So far I have not found an answer on any other post. Here is a sample of the code which I wrote for testing and returns the same exception.
private static final String URL = "jdbc:derby:EDBTest;create=true;";
private static final String USERNAME = "xxx";
private static final String PASSWORD = "xxx";
private Connection connection = null;
public TestEDBM() {
try {
// System.setProperty("derby.system.home", "User");
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException ex) {
Logger.getLogger(TestEDBM.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(TestEDBM.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void createDB() {
try {
PreparedStatement x = connection.prepareStatement(
"CREATE TABLE test(testID INT not null primary key"
+ " GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),"
+ "testName VARCHAR(50))"
);
x.execute();
} catch (SQLException ex) {
Logger.getLogger(TestEDBM.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void addValue(String data) {
int id = -1;
try {
PreparedStatement x = connection.prepareStatement(
"INSERT INTO test testName values (?)", Statement.RETURN_GENERATED_KEYS
);
x.setString(1, data);
id = x.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(TestEDBM.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(id);
}
public String retrieveData() {
ResultSet resultSet = null;
String data = "";
try {
resultSet = connection.createStatement().executeQuery("SELECT * FROM"
+ " test");
resultSet.next();
data = resultSet.getString("testName");
} catch (SQLException ex) {
Logger.getLogger(TestEDBM.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (resultSet != null) {
resultSet.close();
}
} // end try
catch (SQLException sqlException) {
sqlException.printStackTrace();
} // end catch
} // end finally
return data;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
TestEDBM t = new TestEDBM();
// t.createDB();
t.addValue("Tester");
System.out.println(t.retrieveData());
}
}