抛出SQLException,但一切正常

时间:2018-09-28 18:42:17

标签: java sqlite exception-handling sqlexception

我正在将Java代码连接到SQLite数据库,一切正常,但是正在引发异常,并且向用户显示失败消息,为什么我的代码会引发此异常?

public static void main(String[] args) {
    Connection c = null;

    try {
        Class.forName("org.sqlite.JDBC");
        c = DriverManager.getConnection("jdbc:sqlite:admin.db");
        System.out.println("Connection established");
        String cmd = "INSERT INTO admins(user_name, password)"
                + "VALUES('TinoCle','1234')";
        Statement stmt = c.createStatement();
        //Executes my command "cmd"
        stmt.execute(cmd);
        //Sends it to the db
        c.commit();
        //Close connection
        stmt.close();
        c.close();
    } 

    catch (ClassNotFoundException e) {
        e.printStackTrace();
    } 

    catch (SQLException e) {
        //This message is showing up, but it shouldn't
        System.out.println("Connection to DB failed");
        System.exit(0);
    }

}

0 个答案:

没有答案