1、导入mysql包 地址:http://download.csdn.net/download/u010310183/8529489
package test;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class test_1 {
static String Conf_Class, Conf_url, Conf_username, Conf_password, logfile,
listsize, threadnum, Conf_WriteFilePath, pathWithCIDS, pathForUsed;
static int maxnums = 50;
static boolean runtag = true;
public static void main(String [] args) throws ClassNotFoundException, SQLException{
Connection con = null;
Properties properties = new Properties();
InitConf();//加载配置信息
Class.forName(Conf_Class);
con = DriverManager.getConnection(Conf_url, Conf_username, Conf_password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM task");
int colNum = rs.getMetaData().getColumnCount();
List<Map<String, Object>> row = new ArrayList<Map<String, Object>>();
while (rs.next()) {
Map<String, Object> col = new HashMap<String, Object>();
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
//System.out.println( rs.getMetaData().getColumnType(i) );
switch( rs.getMetaData().getColumnType(i) ){
case Types.VARCHAR:
col.put(rs.getMetaData().getColumnName(i), rs.getString(i));
break;
case Types.INTEGER:
col.put(rs.getMetaData().getColumnName(i), rs.getInt(i));
break;
case Types.BLOB:
InputStream in = rs.getBinaryStream(i);
col.put(rs.getMetaData().getColumnName(i), in );
break;
default:
col.put(rs.getMetaData().getColumnName(i), rs.getString(i));
break;
}
}
System.out.println(rs.getString(2));
row.add(col);
}
for(int i = 0 ; i < 10;i++){
java.util.Map mapRole = (java.util.Map)row.get(i);
//System.out.println(mapRole.get("id"));
}
System.out.println(rs.getMetaData().getColumnCount());//表结构中属性的个数
System.out.println(rs.getMetaData().getColumnName(2).toLowerCase());//表结构中 第二个位置 属性的名称
rs.next();//指针后移一位
System.out.println(rs.getString(4));//获取第四个位置的属性 所对应的值
//List<Map<String, Object>> list = rsToList(rs);
//System.out.println(list.size());
// for(int i = 0 ; i < 10;i++){
// java.util.Map mapRole = (java.util.Map)list.get(i);
// System.out.println(mapRole.get("id"));
// }
// if( !list.isEmpty() ){
// System.out.println(list.get(0));
// }
releaseConnection(rs, stmt, con);//关闭连接
}
private static void releaseConnection(ResultSet rs, Statement stmt, Connection connect) throws SQLException{
try {
if (rs != null){
rs.close();
}
if (stmt != null){
stmt.close();
}
if (connect != null && !connect.isClosed()){
connect.close();
}
} catch (SQLException se){
System.out.println("Close the connection encounter error!\n" + se.getMessage());
throw new SQLException("关闭连接异常!");
}
}
public Connection getConnection() throws ClassNotFoundException, SQLException{
Connection con = null;
Properties properties = new Properties();
try {
properties.load(this.getClass().getResourceAsStream("/db.properties"));
String driver = properties.getProperty("sDBDriver");
String url = properties.getProperty("sConnStr");
String user = properties.getProperty("username");
String password = properties.getProperty("password");
try {
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return con;
}
/**
* 加载配置文件 config.properties
*/
public static void InitConf() {
System.out.println("加载配置信息-Start: "
+ new SimpleDateFormat("HH:mm:ss.SSS").format(new Date()));
try {
Properties properties = new Properties();
properties.load(test_1.class
.getResourceAsStream("/config.properties"));
Conf_Class = properties.getProperty("class");
Conf_url = properties.getProperty("outurl");
Conf_username = properties.getProperty("outuser");
Conf_password = properties.getProperty("outpass");
logfile = properties.getProperty("logpath");
logfile = properties.getProperty("log");
listsize = properties.getProperty("listsize");
threadnum = properties.getProperty("threadnum");
maxnums = Integer.parseInt(properties.getProperty("maxnums"));
pathWithCIDS = properties.getProperty("pathWithCIDS");
pathForUsed = properties.getProperty("pathForUsed");
System.out.println(Conf_Class);
System.out.println(Conf_url);
System.out.println(Conf_username);
File fileLog = new File(logfile);
if (!fileLog.exists()) {
fileLog.mkdirs();
}
logfile = logfile + "/"
+ new SimpleDateFormat("yyyyMMdd").format(new Date())
+ ".log";
System.out.println("加载配置信息-End: "
+ new SimpleDateFormat("HH:mm:ss.SSS").format(new Date()));
} catch (Exception e) {
runtag = false;
System.out.println("error in InitConf!");
}
}
}