import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MysqlJDBC {
@SuppressWarnings("rawtypes")
public static List<Map> findsql(String sql){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//第一步:加载MySQL的JDBC的驱动
Class.forName("com.mysql.jdbc.Driver");
//取得连接的 url,能访问MySQL数据库的用户名,密码;数据库名
String url = "jdbc:mysql://192.168.1.10:3306/shaimei";
String user = "cpdmembers";
String password = "cpdmembers";
//第二步:创建与MySQL数据库的连接类的实例
conn = DriverManager.getConnection(url, user, password);
//第三步:用conn创建Statement对象类实例 stmt
stmt = conn.createStatement();
//第四步:执行查询,用ResultSet类的对象,返回查询的结果
//String sql = "select * from rerule";
rs = stmt.executeQuery(sql);
List<Map> list = new ArrayList<Map>();
while(rs.next()){
Map<String, Object> map = new HashMap<String, Object>();
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
for(int i=1;i<count;i++){
String key = rsmd.getColumnLabel(i);
Object value = rs.getObject(i) ;
map.put(key, value);
list.add(map);
}
System.out.println(list); //取得数据库中的数据
return list;
}
} catch (ClassNotFoundException e) {
//加载JDBC错误,所要用的驱动没有找到
System.out.println("驱动加载错误");
}catch (SQLException ex) {
//显示数据库连接错误或查询错误
System.err.println("SQLException:"+ex.getMessage());
}finally {
try{
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(conn != null) {
conn.close();
conn = null;
}
}catch(SQLException e) {
System.err.println("SQLException:"+e.getMessage());
}
}
return null;
}
public static void main(String[] args) {
//MySQL访问包:http://download.csdn.net/detail/u010696272/8006839
System.out.println(findsql("select * from rerule"));
}
}