要查詢表中的所有數(shù)據(jù),可以使用 SQL 語句 SELECT * FROM table_name; 其中 table_name 是你要查詢的表名。以下是一個(gè)使用 JDBC 進(jìn)行查詢的示例代碼:
```java
import java.sql.*;
public class Main {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/database_name";
String username = "username";
String password = "password";
try {
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
String query = "SELECT * FROM table_name";
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
// 獲取每一行數(shù)據(jù)的值
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
在上面的代碼中,將數(shù)據(jù)庫連接信息和查詢語句替換為你自己的數(shù)據(jù)庫信息和表名,然后運(yùn)行代碼,就可以查詢表中的所有數(shù)據(jù)。