package czw;
import java.io.IOException;
import java.io.InputStream;
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.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class DB {
private static String driver;
private static String url;
private static String user;
private static String password;
static{
try {
Properties pro = new Properties();
InputStream in = DB.class.getClassLoader().getResourceAsStream("czw.properties");
pro.load(in);
driver = pro.getProperty("driver");
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public DB(){}
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url,user,password);
}
public static String[] getColumnNames(ResultSet rs) throws SQLException{
String[] columnNames = null;
ResultSetMetaData rsma = rs.getMetaData();
int count = rsma.getColumnCount();
columnNames = new String[count];
for(int i=0;i<count;i++){
columnNames[i] = rsma.getColumnLabel(i+1);
//System.out.println(columnNames[i]);
}
return columnNames;
}
public List<Map<String, Object> > getList(String sql,Object[] args) throws Exception{
List<Map<String, Object> > list = new ArrayList<Map<String, Object> >();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
conn = getConnection();
ps = conn.prepareStatement(sql);
if(null!= args && args.length>0){
for(int i=0;i<args.length;i++){
ps.setObject(i+1, args[i]);
}
}
rs = ps.executeQuery();
String[] columnLabel = getColumnNames(rs);
while(rs.next()){
Map<String, Object> map = new HashMap<String, Object>();
for(int j=0;j<columnLabel.length;j++){
map.put(columnLabel[j], rs.getObject(columnLabel[j]));
}
list.add(map);
}
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException();
}finally{
close(rs,ps,conn);
}
return list;
}
public static void close(ResultSet rs,Statement st, Connection conn){
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws Exception{
DB db = new DB();
// String sql = "select id ID, sname SNAME, pid PID from S";
// sql = "select id ID, sname SNAME, pid PID from S where s.id = ?";
// List list = db.getList(sql, new Object[]{1});
// for(int i=0;i<list.size();i++){
// Map map = (Map)list.get(i);
// System.out.println(map.get("ID")+" , "+map.get("SNAME")+" , "+map.get("PID"));
// }
String sql = "select id , sname SNAME, pid PID from S";
ResultSet rs = db.getConnection().createStatement().executeQuery(sql);
String[] columnNames = db.getColumnNames(rs);
for(int i=0;i<columnNames.length;i++){
System.out.println(columnNames[i]);
}
}
}