直接上代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class TestJDBC {
private final static String DRIVER_CLASS = "com.mysql.jdbc.Driver";
private final static String DRIVER_URL = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
private final static String USERNAME = "xxxx";
private final static String PASSWORD = "xxxx";
public static void main(String args[]){
List<Map<String, Object>> list = queryList();
if(list!=null && !list.isEmpty()){
for(Map<String, Object> map:list){
Set<Map.Entry<String, Object>> set = map.entrySet();
for(java.util.Iterator<Map.Entry<String, Object>> it=set.iterator();it.hasNext();){
Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next();
System.out.println(entry.getKey() + "--->" + entry.getValue());
}
}
}
}
public static List<Map<String, Object>> queryList() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Connection connection = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
// load jdbc driver
Class.forName(DRIVER_CLASS).newInstance();
connection = DriverManager.getConnection(DRIVER_URL, USERNAME, PASSWORD);
String sql = "select * from website where id=?";
pstmt = connection.prepareStatement(sql);
// 传参数
pstmt.setInt(1, 1);
// 执行SQL语句
rs = pstmt.executeQuery();
// 处理查询结果
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
while (rs.next()) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < columnCount; i++) {
String colName = rsmd.getColumnName(i+1);
Object objVal = rs.getObject(colName);
map.put(colName, objVal);
}
list.add(map);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if(rs!=null){
rs.close();
rs = null;
}
if(pstmt!=null){
pstmt.close();
pstmt = null;
}
if(connection!=null){
connection.close();
connection = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class TestJDBC {
private final static String DRIVER_CLASS = "com.mysql.jdbc.Driver";
private final static String DRIVER_URL = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
private final static String USERNAME = "xxxx";
private final static String PASSWORD = "xxxx";
public static void main(String args[]){
List<Map<String, Object>> list = queryList();
if(list!=null && !list.isEmpty()){
for(Map<String, Object> map:list){
Set<Map.Entry<String, Object>> set = map.entrySet();
for(java.util.Iterator<Map.Entry<String, Object>> it=set.iterator();it.hasNext();){
Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next();
System.out.println(entry.getKey() + "--->" + entry.getValue());
}
}
}
}
public static List<Map<String, Object>> queryList() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Connection connection = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
// load jdbc driver
Class.forName(DRIVER_CLASS).newInstance();
connection = DriverManager.getConnection(DRIVER_URL, USERNAME, PASSWORD);
String sql = "select * from website where id=?";
pstmt = connection.prepareStatement(sql);
// 传参数
pstmt.setInt(1, 1);
// 执行SQL语句
rs = pstmt.executeQuery();
// 处理查询结果
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
while (rs.next()) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < columnCount; i++) {
String colName = rsmd.getColumnName(i+1);
Object objVal = rs.getObject(colName);
map.put(colName, objVal);
}
list.add(map);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if(rs!=null){
rs.close();
rs = null;
}
if(pstmt!=null){
pstmt.close();
pstmt = null;
}
if(connection!=null){
connection.close();
connection = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
}