67,514
社区成员
发帖
与我相关
我的任务
分享
public static void getConnect(){
String username = "root";
String password = "123";
String url = "jdbc:mysql://localhost:3306/test";
String driver = "com.mysql.jdbc.Driver";
Connection con = null;
PreparedStatement stat = null;
ResultSet rs = null;
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url,username,password);
String sql = "select * from user where name = ? ";
stat = con.prepareStatement(sql);
stat.setString(1, "11");
rs = stat.executeQuery();
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
ResultSetMetaData data = rs.getMetaData();
int count = data.getColumnCount();
while(rs.next()){
Map<String, Object> rowData = new HashMap<String, Object>();
for (int i = 1; i < count; i++) {
rowData.put(data.getColumnName(i),rs.getObject(i));
}
list.add(rowData);
}
System.out.println(list);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}