我正在尝试创建一个简单的SQLite数据库程序,该程序可以创建数据库,表,插入和检索数据。我怎样才能解决这个问题?我不断收到异常:
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)