Apache Derby - java.sql.SQLException: Failed to start database
Your program contains some misunderstandings and wrong assignments !
From your image you can see your DATABASE = contactDB not contact.
"jdbc:derby:C:/Users/yohan/.netbeans-derby/contact" is wrong
"jdbc:derby:C:/Users/yohan/.netbeans-derby/contactDB" now only the DB is correct.
Error: Another instance of Derby may have already booted the database C:\Users\yohan.netbeans-derby\contact.For the Con.String it's better not to use the PATH
will work "jdbc:derby://localhost:1527/C:/Users/yohan/.netbeans-derby/contactDB","yohan","xyz"
better is "jdbc:derby://localhost:1527/contactDB","yohan","xyz"
It is better to specify the full Path to the table. Otherwise it is working only in Netbeans, with an already open table and a default schema.
- prepareStatement
only inside Netbeans con.prepareStatement("insert into FRIENDS values(?,?,?)");
better is con.prepareStatement("insert into APP.FRIENDS values(?,?,?)");
Now look at this !
ps.setInt(1, id);
ps.setString(1, firstName);
ps.setString(2, lastName);
- First you set the id with setInt().
- Second Now you set the id with a string setString() ( firstName !!!!!)
- Third at the end you override firstName with lastName and the lastName in the ps. is empty.
This will give you the next Error
It is better , when an error occurs, not to run the program further. The error list will grow up. It's hard to read. in your case the first Error is in createConnection and runs on !!
- Error createConnection
- Error con.prepareStatement("...
- Error ps.executeUpdate()
- Error closeConnection()
Test it with boolean and if !
private boolean createConnection()
{
try
{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
con = DriverManager.getConnection("jdbc:derby://localhost:1527/contactDB","yohan","xyz");
}
catch(Exception e)
{
System.out.println("Error getConnection");
return false;
}
return true;
}
No need to run further if the connection fails.
public void insertData(int id, String firstName, String lastName)
{
if (createConnection()) {
try
{
PreparedStatement ps = con.prepareStatement("INSERT INTO APP.FRIENDS values(?,?,?)");
ps.setInt(1, id);
ps.setString(2, firstName);
ps.setString(3, lastName);
[...]
I hope this helps you a bit.
your datasae is already connected disconnect right click -> disconnect now try it ,
it will work sure.