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;
}
}今天的文章jdbc的数据库驱动类DriverManager.getConnection()代码示例分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/28317.html