private static String driverName = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3307/test?useUnicode=true&characterEncoding=UTF-8";
private static String user = "root";
private static String password = "root";
Class.forName(driverName);
// Connection conn = DriverManager.getConnection(url, user, password);//根据URL连接指定的数据库
Driver driver = DriverManager.getDriver(url);
java.util.Properties info = new java.util.Properties();
info.put("user", user);
info.put("password", password);
Connection conn = driver.connect(url, info);
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("select id as `主键`,user_name as `名字` from user");
ResultSetMetaData metaData = rs.getMetaData();
//查询查询的字段和别名的对应关系
for(int i=1;i<=metaData.getColumnCount();i++){
System.out.println(metaData.getColumnLabel(i)+" "+metaData.getColumnName(i)+" "
+metaData.getColumnType(i)+" "+metaData.getColumnTypeName(i));
}
System.out.println("============================================================");
while(rs.next()){
for(int i=1;i<=metaData.getColumnCount();i++){
//获取别名和 值
System.out.println(metaData.getColumnLabel(i)+" "+rs.getObject(i));
}
}
通过jdbc获取查询sql的别名 和 值
最新推荐文章于 2023-08-08 00:00:51 发布