private static String dbName = "";
private static String user = "";
private static String password = "";
private static String url = "jdbc:sqlserver://localhost:1433;databaseName="+dbName+";user="+user+";password="+password;
private static DataSource dataSource = null;
static{
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (Exception e){
System.out.println("Exception:"+e.getMessage()+"");
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection() throws SQLException{
Connection conn = DriverManager.getConnection(url);
return conn;
}
public static DataSource getDataSource(){
return dataSource;
}
public static PreparedStatement prepareStatement(Connection con, String sql) {
PreparedStatement stmResult = null;
try {
stmResult = con.prepareStatement(sql);
} catch (Exception e) {
}
return stmResult;
}
public static void free(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{
try{
if(conn != null){
conn.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
final static public String SELECT = "SELECT * FROM tablename";
public static void main(String[] args) {
try {
long begin = System.currentTimeMillis();
Connection connection = getConnection();
PreparedStatement psPreparedStatement = connection.prepareStatement(SELECT);
ResultSet resultSet =psPreparedStatement.executeQuery();
int cc = 0;
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
if(null != resultSet){
ResultSetMetaData mtdResults = resultSet.getMetaData();
cc = mtdResults.getColumnCount();
while(resultSet.next()){
Map<String, Object> map = new HashMap<String, Object>();
for(int i= 1;i<=cc;i++){
String colnameString = mtdResults.getColumnLabel(i);
int type = mtdResults.getColumnType(i);
Object colValue = resultSet.get...(i);
map.put(colnameString, colValue);
}
list.add(map);
}
}
free(resultSet, psPreparedStatement, connection);
long end = System.currentTimeMillis();
long diff = end - begin;
System.out.println(diff);
} catch (SQLException e) {
e.printStackTrace();
}
}
这个是转自他http:
switch(type){
case Types.LONGVARCHAR:
dataType="Long";
break;
case Types.CHAR:
dataType="Character";
break;
case Types.NUMERIC:
switch(scale){
case 0:
dataType="Number";
break;
case -127:
dataType="Float";
break;
default:
dataType="Number";
}
break;
case Types.VARCHAR:
dataType="String";
break;
case Types.DATE:
dataType="Date";
break;
case Types.TIMESTAMP:
dataType="Date";
break;
case Types.BLOB :
dataType="Blob";
break;
default:
dataType="String";
}