简体   繁体   English

java.sql.SQLException:语句未执行

[英]java.sql.SQLException: statement is not executing

I am trying to create a simple SQLite database program that could create a database, a table, insert and retrieve data. 我正在尝试创建一个简单的SQLite数据库程序,该程序可以创建数据库,表,插入和检索数据。 How can I fix this? 我怎样才能解决这个问题? I keep getting the exception: 我不断收到异常:

java.sql.SQLException: statement is not executing java.sql.SQLException:语句未执行

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)

Dont know whether you found the solution. 不知道您是否找到解决方案。 The reason for this is most probably that the statement ( st ) is still referring to Create Command when you call st.executeUpdate(insertcontacts) . 这样做的原因很可能是,当您调用st.executeUpdate(insertcontacts)时,语句( st )仍然引用Create Command

Try to reinitialize the statement and run the insert command. 尝试重新初始化该语句并运行insert命令。 It should work now! 现在应该可以工作了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM