JDBC學習小結(一)
阿新 • • 發佈:2019-01-24
## JDBC學習小結 (一)###
一:什麼是JDBC
JDBC(Java DataBase Connectivity,java資料庫連線)是一種用於執行SQL語句的Java API,可以為多種關係資料庫提供統一訪問,它由一組用Java語言編寫的類和介面組成。JDBC提供了一種基準,據此可以構建更高階的工具和介面,使資料庫開發人員能夠編寫資料庫應用程式。
二:JDBC中主要設計的類與介面及其作用
JDBC API 提供的介面如下
-
三、在普通Java程式中實現資料庫的連線的基本步驟
//資料庫的連線資訊
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver" ;
static final String DB_URL = "jdbc:mysql://localhost:3306/example";
static final String USER = "root";
static final String PASS = "****";
Connection conn = null;
Statement stmt = null;
//註冊驅動
Class.forName(JDBC_DRIVER);
//建立資料庫的連線
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
//編寫sql語句
String sql = "SELECT NAME FROM `tbl_jxutcmeap_user` WHERE user_id='user20180226153614546'";
//執行查詢
ResultSet rs =stmt.executeQuery(sql);
//處理結果
String name = null ;
while(rs.next()){
name= rs.getString("name");
System.out.println(name);
}
//關閉連線
rs.close();
stmt.close();
conn.close();
四、嘗試封裝自己的工具類
public class JdbcUtilImpl implements JdbcUtil {
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs =null;
private void getConnection(){
Properties prop = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream("D:\\java\\workspace\\TomcatTest\\WebContent\\jdbc.properties"));
prop.load(in); ///載入屬性列表
String driver = prop.getProperty("driver");
String user = prop.getProperty("user");
String pass = prop.getProperty("pass");
String dbUrl = prop.getProperty("dbUrl");
in.close();
Class.forName(driver);
conn = DriverManager.getConnection(dbUrl,user,pass);
} catch (Exception e) {
e.printStackTrace();
System.out.println("get connection default......");
}
}
@Override
public void close(){
try{
if(null != rs)
rs.close();
if(null != stmt)
stmt.close();
if(null != conn)
conn.close();
}catch(Exception e){}
}
@Override
public boolean update(String sql, Object... params) {
getConnection();
StringBuilder sb = new StringBuilder(sql);
try {
stmt = conn.createStatement();
//處理sql
for(int i=0 ;i<params.length;i++){
String param = params[i].toString();
if(params[i] instanceof String){
param = "'"+param+"'";
}
int start = sb.indexOf("?");
sb.replace(start, start+1, param);
}
int num =stmt.executeUpdate(sb.toString());
close();
if(num <=0){
return false;
}
} catch (Exception e) {
e.printStackTrace();
close();
return false;
}
return true;
}
@Override
public ResultSet query(String sql, Object... params) {
getConnection();
StringBuilder sb = new StringBuilder(sql);
try{
stmt = conn.createStatement();
//處理sql
for(int i=0 ;i<params.length;i++){
String param = params[i].toString();
if(params[i] instanceof String){
param = "'"+param+"'";
}
int start = sb.indexOf("?");
if(start==-1)
break;
sb.replace(start, start+1, param);
}
ResultSet rs = stmt.executeQuery(sb.toString());
return rs;
}catch(Exception e){
System.out.println(e.getMessage());
}
return rs;
}
}
五、對在封裝工具類中遇到的問題的總結
- 首先是程式碼的優化:如建立連線過程中載入配置檔案資料的部分程式碼可以使用靜態程式碼塊進行載入,這部分內容一般不會變,只需在載入類的時候執行一次即可。
- 同時載入配置檔案(.properties)可以使用ResourceBundle類來載入(注意:使用ResourceBundle讀取配置檔案,預設要把配置檔案放在classpath下(預設的src目錄下))
將程式碼調整如下:
private static String driver; // 驅動
private static String dbUrl;
private static String user;
private static String pass;
static{
ResourceBundle buldle = ResourceBundle.getBundle("jdbc");
driver = buldle.getString("driver");
user = buldle.getString("user");
pass = buldle.getString("pass");
dbUrl = buldle.getString("dbUrl");
}
- 執行的SQL語句中出現中文導致語句執行失敗
解決的辦法:在dburl後加上?useUnicode=true&characterEncoding=utf8
driver=com.mysql.jdbc.Driver
dbUrl=jdbc:mysql://127.0.0.1:3306/example?useUnicode=true&characterEncoding=utf8
user=root
pass=****
未完待續。。。