jdbc到MYSQL错误:“表airportdetails不存在" [英] jdbc to MYSQL error: "table airportdetails doesn't exist"
问题描述
我正在尝试使用后端中的jdbc从jsp页面连接到MySQL数据库.
我有以下代码:
I am trying to connect to a MySQL database from a jsp page using jdbc in the backend.
I have the following code:
public static void insertIntoDatabase(String code,String name,String temp,String hum,String del) {
Connection con = null;
if (del.length() == 0) {
del="no data";
}
name = name.replaceAll("\\(.+?\\)", "");
name = name.replaceAll(" ", "_");
del = del.replaceAll(" ", "_");
System.out.println("del "+del);
String url = "jdbc:mysql://localhost:3306/test";
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url,"root","");
con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS aiportdetails(code VARCHAR(50) PRIMARY KEY, " +
"name VARCHAR(250), temp VARCHAR(50), hum VARCHAR(50), del VARCHAR(50))");
ResultSet rs = con.prepareStatement("SELECT * FROM airportdetails;").executeQuery();
} catch (SQLException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (con != null) {
con.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
我在
ResultSet rs = con.prepareStatement("SELECT * FROM airportdetails;").executeQuery();
ResultSet rs = con.prepareStatement("SELECT * FROM airportdetails;").executeQuery();
错误:
表"test.airportdetails"不存在
Table 'test.airportdetails' doesn't exist
但是从我的phpmyadmin中,我可以看到该表已创建并存在:
我收到此错误的原因是什么?
谢谢.
But from my phpmyadmin I can see that the table is created and exists:
What is the reason I am getting this error?
Thank you.
推荐答案
executeUpdate()
在此PreparedStatement对象中执行SQL语句,该对象必须是SQL INSERT
,UPDATE
或DELETE
语句;或不返回任何内容的SQL语句,例如DDL语句.
executeUpdate()
Executes the SQL statement in this PreparedStatement object, which must be an SQL INSERT
, UPDATE
or DELETE
statement; or an SQL statement that returns nothing, such as a DDL statement.
当前,您正在尝试使用它来创建表.这就是为什么您会收到该错误的原因.
Currently you are trying to use this for creating a table. That's the reason why you are getting that error.
请参阅文档 Java 1.4.2 用于 executeUpdate
您应该使用Statement
Statement st = con.createStatement();
String table = "Create table .......";
st.executeUpdate(table);
这篇关于jdbc到MYSQL错误:“表airportdetails不存在"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!