1. 程式人生 > >jdbc的資料庫驅動類DriverManager.getConnection()程式碼示例

jdbc的資料庫驅動類DriverManager.getConnection()程式碼示例

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.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Test{
private static final String DRIVER_CLASS = "oracle.jdbc.driver.OracleDriver";
private static final String DATABASE_URL = "jdbc:oracle:thin:@10.12.23.345:1234:abcd";
private static final String DATABASE_USER = "user";
private static final String DATABASE_PASSWORD = "password";
private static Connection conn = null;
private static final String SQL_SELECT = "SELECT * FROM TABLE";

private static final String SQL_UPDATE = "UPDATE TABLE SET column= ? WHERE P_ID = ?";

public static Connection getConnection(){
try{
Class.forName(DRIVER_CLASS);
conn = DriverManager.getConnection(DATABASE_URL, DATABASE_USER, DATABASE_PASSWORD);
return conn;
}catch(Exception e){
e.printStackTrace();
}
return conn;
}

public static void main(String[] args) throws SQLException {
conn = getConnection();
PreparedStatement preparedStatement = conn.prepareStatement(SQL_SELECT);

ResultSet rs = preparedStatement.executeQuery();
Map<String, Object> temp = null;

List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();

                //獲取列名

List<Map<String,Object>> columns = getColumnLabels(rs);
while(rs.next()){
temp = new HashMap<String, Object>();
for (Map<String,Object> tempMap : columns) {
            if(Types.NUMERIC == Integer.parseInt(tempMap.get("columntype")+"")){
            double value = rs.getDouble(tempMap.get("columnlabel").toString());
            temp.put(tempMap.get("columnlabel").toString(), value);
            }else if(Types.CLOB == Integer.parseInt(tempMap.get("columntype")+"")) {
            oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob(tempMap.get("columnlabel").toString());
   if(clob!=null && clob.length()>0)   
   { 

   temp.put(tempMap.get("columnlabel").toString(), 

                                        clob.getSubString((long)1,(int)clob.length()));

   }
            }else{
            Object value = rs.getObject(tempMap.get("columnlabel").toString());
            temp.put(tempMap.get("columnlabel").toString(), value);
            }
                
            }
list.add(temp);
}

List<Map<String, Object>>name = mergeName(list);

PreparedStatement ps = conn.prepareStatement(SQL_UPDATE);
for(Map<String, Object> obj:name){
ps.setString(1, obj.get("spm")+"");
ps.setString(2, obj.get("id")+"");
ps.addBatch();
}
ps.executeBatch();
ps.close();
conn = getConnection();
System.out.println("更新完成");
}

private static List<Map<String,Object>> getColumnLabels(ResultSet resultSet)
            throws SQLException {
        List<Map<String,Object>> labels = new ArrayList<Map<String,Object>>();

        ResultSetMetaData rsmd = (ResultSetMetaData) resultSet.getMetaData();
        Map<String,Object> tempMap;
        for (int i = 0; i < rsmd.getColumnCount(); i++) {
        tempMap=new HashMap<String, Object>();
        tempMap.put("columnlabel", rsmd.getColumnLabel(i + 1).toLowerCase());
        tempMap.put("columntype", rsmd.getColumnType(i + 1));
            labels.add(tempMap);
        }

        return labels;
    }


private static List<Map<String, Object>> mergeName(List<Map<String, Object>> list) {
return null;
}

}