我不知道运行文件何时不起作用。
数据库连接
public Connection connect() {
Connection conn = null;
try {
// db parameters
// create a connection to the database
conn = DriverManager.getConnection("jdbc:sqlite:airport.sqlite");
System.out.println("Connection to SQLite has been established.");
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
return null;
}
插入表
@FXML
private void insertIntoAirport(ActionEvent event) throws SQLException {
Connection conn = this.connect();
PreparedStatement pstmt=null;
try{
String url="INSERT INTO airport VALUES(?)";
pstmt=conn.prepareStatement(url);
// pstmt=conn.prepareStatement("INSERT INTO airport VALUES()");
pstmt.setString(1,city.getText() );
pstmt.execute();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}}
并在此代码中输出给我错误 我不明白为什么它不起作用
pstmt=conn.prepareStatement(url);
答案 0 :(得分:0)
可能是您的连接URL“ jdbc:sqlite:airport.sqlite”出了问题。可能是由于该原因,无法建立连接,并且“ conn”变为空。在“ jdbc:sqlite”部分之后,提供您数据库的绝对路径。粘贴您将获得更多想法的例外。