You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
3.6 KiB
143 lines
3.6 KiB
package com.fr.plugin.it.utils; |
|
|
|
import oracle.jdbc.driver.OracleDriver; |
|
import java.sql.*; |
|
import java.util.*; |
|
|
|
public class JDBCUtil { |
|
private static void release(Connection conn,Statement st,ResultSet rs) { |
|
closeRs(rs); |
|
closeSt(st); |
|
closeConn(conn); |
|
} |
|
|
|
private static void closeRs(ResultSet rs) { |
|
try { |
|
if(rs!=null) { |
|
rs.close(); |
|
} |
|
} catch (SQLException e) { |
|
e.printStackTrace(); |
|
} finally { |
|
rs=null; |
|
} |
|
|
|
} |
|
|
|
private static void closeSt(Statement st) { |
|
try { |
|
if(st!=null) { |
|
st.close(); |
|
} |
|
} catch (SQLException e) { |
|
e.printStackTrace(); |
|
} finally { |
|
st=null; |
|
} |
|
|
|
} |
|
|
|
private static void closeConn(Connection conn) { |
|
try { |
|
if(conn!=null) { |
|
conn.close(); |
|
} |
|
} catch (SQLException e) { |
|
e.printStackTrace(); |
|
} finally { |
|
conn=null; |
|
} |
|
|
|
} |
|
|
|
/** |
|
* 获取数据库连接 |
|
* @param url |
|
* @param user |
|
* @param password |
|
* @return |
|
*/ |
|
private static Connection getConnection(String url,String user,String password){ |
|
Connection conn = null; |
|
|
|
try{ |
|
//oracle |
|
DriverManager.registerDriver(new OracleDriver()); |
|
//sqlService |
|
//DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver()); |
|
conn =DriverManager.getConnection(url,user , password);} |
|
catch(Exception e){ |
|
FRUtils.FRLogInfo("获取数据库连接异常:"+e.getMessage()); |
|
} |
|
|
|
return conn; |
|
} |
|
|
|
public static List<Map<String,Object>> getResult(String sql, String[] columns){ |
|
List<Map<String,Object>> result = new ArrayList<Map<String,Object>>(); |
|
|
|
Connection conn=null; |
|
Statement st=null; |
|
ResultSet rs=null; |
|
|
|
try { |
|
String url = ""; |
|
String user = ""; |
|
String password = ""; |
|
|
|
conn = getConnection(url,user,password); |
|
st = conn.createStatement(); |
|
rs = st.executeQuery(sql); |
|
//遍历查询每条记录 |
|
while(rs.next()) { |
|
Map<String,Object> map = new HashMap<String,Object>(); |
|
|
|
for(String column : columns){ |
|
map.put(column,rs.getString(column)); |
|
} |
|
|
|
result.add(map); |
|
} |
|
} catch (SQLException e) { |
|
FRUtils.FRLogInfo("获取数据异常:"+e.getMessage()); |
|
} finally { |
|
release(conn, st, rs); |
|
} |
|
|
|
return result; |
|
} |
|
|
|
/** |
|
* 更新 |
|
* @param sql |
|
* @return |
|
*/ |
|
public static int update(String sql){ |
|
FRUtils.FRLogInfo("sql:"+sql); |
|
Connection conn=null; |
|
Statement st=null; |
|
int count = 0; |
|
|
|
Properties p = PropertiesUtils.getProperties2("/resources/database.properties"); |
|
|
|
try { |
|
DriverManager.registerDriver(new OracleDriver()); |
|
String url = p.getProperty("url"); |
|
String user = p.getProperty("username"); |
|
String password = p.getProperty("psd"); |
|
|
|
FRUtils.FRLogInfo("url:"+url+"user:"+user+"password:"+password); |
|
|
|
conn = DriverManager.getConnection(url,user , password); |
|
st = conn.createStatement(); |
|
count = st.executeUpdate(sql); |
|
|
|
} catch (SQLException e) { |
|
FRUtils.FRLogInfo("添加数据异常:"+e.getMessage()); |
|
} finally { |
|
JDBCUtil.release(conn, st, null); |
|
} |
|
|
|
return count; |
|
} |
|
}
|
|
|