packageutil;importjava.sql.Connection;importjava.sql.Driver;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.ResultSetMetaData;importjava.sql.SQLException;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;public classDateExecute {public staticConnection getConnection(String name, String password)throwsInstantiationException, IllegalAccessException,
ClassNotFoundException, SQLException {
Connection con;
String driverName= "com.mysql.jdbc.Driver";
Driver d=(Driver) Class.forName(driverName).newInstance();
con= DriverManager.getConnection("jdbc:mysql://localhost:3307/nona",
name, password);returncon;
}public static List>getDateList(String sql)throwsInstantiationException, IllegalAccessException,
ClassNotFoundException, SQLException {
Connection conn= getConnection("root", "root");
PreparedStatement stmt;
List> list = new ArrayList>();try{
stmt=conn.prepareStatement(sql);
ResultSet rs=stmt.executeQuery(sql);
list=convertList(rs);
}catch(SQLException e) {
System.out.println("数据库连接失败");
e.printStackTrace();
}returnlist;
}private staticList convertList(ResultSet rs)throwsSQLException {
List list= newArrayList();
ResultSetMetaData md=rs.getMetaData();int columnCount = md.getColumnCount(); //Map rowData;
while (rs.next()) { //rowData = new HashMap(columnCount);
Map rowData = new HashMap();for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));
}
list.add(rowData);
}returnlist;
}public static intexecuteUpdate(String sql)throwsInstantiationException, IllegalAccessException,
ClassNotFoundException, SQLException {
Connection conn= getConnection("root", "root");
PreparedStatement stmt;int success = 0;try{
stmt=conn.prepareStatement(sql);
success=stmt.executeUpdate(sql);
}catch(SQLException e) {
System.out.println("数据库连接失败");
e.printStackTrace();
}returnsuccess;
}
}