import java.sql.*;
public class DatabaseQuery {
public static void main(String[] args) {
// 数据库连接配置
String url = "jdbc:mysql://localhost:3306/your_database";
String user = "your_username";
String password = "your_password";
// 两列数据的变量
String column1Name = "column1";
String column2Name = "column2";
try (Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT " + column1Name + ", " + column2Name + " FROM your_table")) {
// 遍历结果集
while (rs.next()) {
// 获取并打印两列数据
String column1Data = rs.getString(column1Name);
String column2Data = rs.getString(column2Name);
System.out.println(column1Data + ", " + column2Data);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
jdbc查询2个数据如何显示2个数据
最新推荐文章于 2024-11-03 11:01:20 发布
本文展示了如何使用Java的JDBCAPI从名为your_database的MySQL数据库表中查询两列数据(column1和column2),并捕获SQLException异常。
摘要由CSDN通过智能技术生成