The method Statement() is undefined for the type Connection
时间: 2024-05-10 13:19:44 浏览: 121
The error message "The method Statement() is undefined for the type Connection" typically occurs when you try to call the `Statement()` method on a `Connection` object in Java, but the method is not defined for that class.
To fix this error, you need to create a `Statement` object separately using the `createStatement()` method of the `Connection` object. Here is an example:
```
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM mytable");
```
In this example, the `getConnection()` method is used to establish a connection to the MySQL database. Then, the `createStatement()` method is called on the `conn` object to create a `Statement` object, which is used to execute a query on the database.
Make sure you have imported the necessary classes for `Connection`, `Statement`, and `ResultSet`.
阅读全文