[英]error with instantiating a SQL statement (Java) (JDBC)
I've been trying to practice using JDBC in netbeans but i'm stuck with a little problem, now i loaded a driver, established a connection to SQL but for some reason my SQL statement isn't working, i'll be glad if anyone can bear with me 我一直在尝试在netbeans中使用JDBC,但是我遇到了一个小问题,现在我加载了一个驱动程序,建立了与SQL的连接,但是由于某种原因我的SQL语句无法正常工作,如果能够任何人都可以忍受我
public void dbTest() {
try {
Class.forName(
"com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException |
IllegalAccessException |
ClassNotFoundException e) {
}
try (Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/", "root", "explore");
Statement statement = (Statement) connection.createStatement()) {
String query = "select first_name, last_name"
+ " from sakila.customer "
+ "where address_id < 10";
try (ResultSet resultset =
statement.executeQuery(query)) {
while (resultset.next()) {
String firstName =
resultset.getString("first_name");
String lastName =
resultset.getString("last_name");
System.out.println(firstName + " " + lastName);
}
}
} catch (SQLException e) {
}
}
this line of code is causing me the trouble 这行代码给我带来了麻烦
Statement statement = (Statement) connection.createStatement()
Thanks!! 谢谢!!
As most of the comments states, you should use java.sql.Statement
and not java.beans.Statement
. 正如大多数注释所指出的那样,您应该使用java.sql.Statement
而不是java.beans.Statement
。
Another thing i notice with your code is that you are querying and you have not specified from which database these operation should be done which will lead you to SQLException
. 我在代码中注意到的另一件事是您正在查询,但尚未指定应从哪个数据库执行这些操作,这将导致您进入SQLException
。 So again you will have to change the url string from "jdbc:mysql://localhost:3306/"
to "jdbc:mysql://localhost:3306/database_name"
. 因此,再次必须将url字符串从"jdbc:mysql://localhost:3306/"
更改为"jdbc:mysql://localhost:3306/database_name"
。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.