您好,登錄后才能下訂單哦!
可以建一個(gè)properties的文件
jdbc.username=scott
jdbc.password=tiger
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
然后建立一個(gè)JDBCUtil類
package com.imooc.page.util;
import java.io.InputStream;
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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class JdbcUtil {
// 表示定義數(shù)據(jù)庫的用戶名
private static String USERNAME;
// 定義數(shù)據(jù)庫的密碼
private static String PASSWORD;
// 定義數(shù)據(jù)庫的驅(qū)動(dòng)信息
private static String DRIVER;
// 定義訪問數(shù)據(jù)庫的地址
private static String URL;
// 定義數(shù)據(jù)庫的鏈接
private Connection connection;
// 定義sql語句的執(zhí)行對(duì)象
private PreparedStatement pstmt;
// 定義查詢返回的結(jié)果集合
private ResultSet resultSet;
static {
// 加載數(shù)據(jù)庫配置信息,并給相關(guān)的屬性賦值
loadConfig();
}
/**
* 加載數(shù)據(jù)庫配置信息,并給相關(guān)的屬性賦值
*/
public static void loadConfig() {
try {
InputStream inStream = JdbcUtil.class
.getResourceAsStream("/jdbc.properties");
Properties prop = new Properties();
prop.load(inStream);
USERNAME = prop.getProperty("jdbc.username");
PASSWORD = prop.getProperty("jdbc.password");
DRIVER = prop.getProperty("jdbc.driver");
URL = prop.getProperty("jdbc.url");
/*
* jdbc.username=scott jdbc.password=tiger
* jdbc.driver=oracle.jdbc.driver.OracleDriver
* jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
*/
} catch (Exception e) {
throw new RuntimeException("讀取數(shù)據(jù)庫配置文件異常!", e);
}
}
public JdbcUtil() {
}
/**
* 獲取數(shù)據(jù)庫連接
*
* @return 數(shù)據(jù)庫連接
*/
public Connection getConnection() {
try {
Class.forName(DRIVER); // 注冊(cè)驅(qū)動(dòng)
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); // 獲取連接
} catch (Exception e) {
throw new RuntimeException("get connection error!", e);
}
return connection;
}
/**
* 執(zhí)行更新操作
*
* @param sql
* sql語句
* @param params
* 執(zhí)行參數(shù)
* @return 執(zhí)行結(jié)果
* @throws SQLException
*/
public boolean updateByPreparedStatement(String sql, List params)
throws SQLException {
boolean flag = false;
int result = -1;// 表示當(dāng)用戶執(zhí)行添加刪除和修改的時(shí)候所影響數(shù)據(jù)庫的行數(shù)
pstmt = connection.prepareStatement(sql);
int index = 1;
// 填充sql語句中的占位符
if (params != null && !params.isEmpty()) {
for (int i = 0; i < params.size(); i++) {
pstmt.setObject(index++, params.get(i));
}
}
result = pstmt.executeUpdate();
flag = result > 0 ? true : false;
return flag;
}
/**
* 執(zhí)行查詢操作
*
* @param sql
* sql語句
* @param params
* 執(zhí)行參數(shù)
* @return
* @throws SQLException
*/
public List<map> findResult(String sql, List params)
throws SQLException {
List<map> list = new ArrayList<map>();
int index = 1;
pstmt = connection.prepareStatement(sql);
if (params != null && !params.isEmpty()) {
for (int i = 0; i < params.size(); i++) {
pstmt.setObject(index++, params.get(i));
}
}</map</map</map
resultSet = pstmt.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int cols_len = metaData.getColumnCount();
while (resultSet.next()) {
Map map = new HashMap();
for (int i = 0; i < cols_len; i++) {
String cols_name = metaData.getColumnName(i + 1);
Object cols_value = resultSet.getObject(cols_name);
// System.out.println("cols_name:"+cols_name+" cols_value:"+cols_value);
if (cols_value == null) {
cols_value = "";
}
// object類型需要轉(zhuǎn)換成integer類型
map.put(cols_name.toLowerCase(), cols_value);
}
list.add(map);
/*
* for(int i=0;i<cols_len;i++){ } */
}
return list;
}
public void insertResult(String sql)
throws SQLException {
pstmt = connection.prepareStatement(sql);
try {
pstmt.execute(sql);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 釋放資源
*/
public void releaseConn() {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/*
* public static void main(String[] args) { JdbcUtil jdbcUtil = new
* JdbcUtil(); jdbcUtil.getConnection(); System.out.println("2-----"); try {
* List<map> result = jdbcUtil.findResult(
* "select * from t_student", null); for (Map m : result) {
* System.out.println(m); } } catch (SQLException e) { e.printStackTrace();
* } finally { jdbcUtil.releaseConn(); } }
*/
}</map
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。