I'm trying SQLite with Java, this is the first time using both together, here is the code:
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
/**
*
* @author Alessio
*/
public class DB {
public static void main(String[] args){
Connection c = null;
Statement stmt = null;
try{
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:test.db");
stmt = c.createStatement();
String sql = "CREATE TABLE COMPANY " +
"(ID INT PRIMARY KEY NOT NULL," +
" NAME TEXT NOT NULL, " +
" AGE INT NOT NULL, " +
" ADDRESS CHAR(50), " +
" SALARY REAL)";
stmt.executeUpdate(sql);
stmt.close();
c.close();
}
catch(Exception e){
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Database opened succesfully!!!");
}
}
When I run the code for the first time I have any error, my result in console is:
run:
Database opened succesfully!!!
BUILD SUCCESSFUL (total time: 1 second)
while the second time I get
run:
java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database ()
BUILD SUCCESSFUL (total time: 1 second)
What am I doing wrong?