java.sql.SQLException : statement is not executing

标签 java sqlite

我正在尝试创建一个简单的 SQLite 数据库程序,它可以创建数据库、表、插入和检索数据。我怎样才能解决这个问题?我不断收到异常:

java.sql.SQLException: statement is not executing

package databaseproj;

import java.sql.*;

public class main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try{
            String sDriverName = "org.sqlite.JDBC";
            Class.forName(sDriverName);
            String dbURL= "jdbc:sqlite:hello.db";

            Connection conn= DriverManager.getConnection(dbURL);
            Statement st= conn.createStatement();

            String createtable="CREATE TABLE contacts(name text, number numeric, email text)";
            System.out.println("Table created successfully");
            String insertcontacts="INSERT INTO contacts(name, number, email) VALUES('nduka',08166459353,'ndukaude')";

            st.executeUpdate(createtable);
            st.executeUpdate(insertcontacts);
            System.out.println("Insert complete");

            ResultSet rs= st.getResultSet(); 

            conn.setAutoCommit(false);
            while ( rs.next() ){
                String name= rs.getString("name");
                int num=rs.getInt("number");
                String email=rs.getString("email");

                System.out.println("NAME: "+name);
                System.out.println("NUMBER: "+ num);
                System.out.println("EMAIL: "+email);
            }

            conn.commit();
            conn=null;
            System.out.println("connection emptied");
            rs.close();

            st.close();

            conn.close();
            System.out.println("connection closed");
        }

        catch ( Exception e ) {
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
            System.exit(0);
        }
    }
}

最佳答案

使用execute(createtable)代替executeUpdate(createtable)

关于java.sql.SQLException : statement is not executing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34353503/

相关文章:

sqlite - 如何在 Flutter 中使用 Sqlite 将 bool 值转换为 Int 值?

sqlite - 如何防止 SQLite 数据库锁定?

java - 使用 MediaRecorder 在 Android 上录制屏幕时无法获取 Surface

java - 如何在数组内用填充长度填充字符串?

java - 检索猫头鹰 :restrictions using the OWL API

java - Java 程序的退出查询

java - Room Persistence 库 - 带有 List<Video> 的嵌套对象,@Embedded 不起作用。

Android Sqlite IN,NOT IN 语法

sql - 基于概念模式编写 sql 脚本

java - 从 Java Activity 类调用自定义 View 的 Kotlin 类中的动画