JDBC(推薦學(xué)習(xí)網(wǎng)站:stackoverflow)
在這里先給出可能會(huì)用到的資源包括以下:
- MySql驅(qū)動(dòng)jar包
- Oracle驅(qū)動(dòng)jar包
- navicatMySql數(shù)據(jù)庫的可視化工具(navicat110_mysql_cs_x86為中文,另一個(gè)英文结缚,英文沒有破解說明,不會(huì)破解的可以在下方評(píng)論)
- Oracle數(shù)據(jù)庫的可視化工具
- mySql安裝包和安裝說明(mySql資料有安裝詳細(xì)說明)
- oracle安裝包和安裝說明蜗帜,這個(gè)Oracle比較写先(對(duì)于學(xué)習(xí)來說足夠了)熏矿,而且卸載方便不必像其它安裝包的oracle艰亮,安裝之后難以卸載干凈装处。
需要的點(diǎn)擊以下鏈接獲染摹(失效了在下方評(píng)論):JDBC編程工具
sql 標(biāo)準(zhǔn)化查詢語言
sun 【驅(qū)動(dòng)】 數(shù)據(jù)庫廠商
Java語言 mysql
oracle
標(biāo)準(zhǔn) 實(shí)現(xiàn)
數(shù)據(jù)庫連接步驟:
-
注冊(cè)驅(qū)動(dòng)
mysql驅(qū)動(dòng)
oracle驅(qū)動(dòng)
-
...
Class.forName(driver);
-
獲取連接
連接 url
url
jdbc:oracle:thin:@localhost:1521:XE
jdbc:mysql://127.0.0.1:3306/zmysql
user root
password rootConnection conn = ConnectionFactory.getConnection();
-
創(chuàng)建pstmt/stmt對(duì)象
如果有占位符替換占位符String sql = "insert into tbl_student values(null,'"+stu.getName()+"',"+stu.getAge()+")"; pstmt = conn.prepareStatement(sql);
-
執(zhí)行sql
int executeUpdate(); 增 刪 該 Result executeQuery(); 查 execute();
-
如果有結(jié)果集,處理結(jié)果集
while(rs.next()){ long id = rs.getLong("id"); String name = rs.getString("name"); int age = rs.getInt("age"); System.out.println(id+"=="+name+"=="+age); }
-
釋放資源
Connection
PreparedStatement
ResultSet
后創(chuàng)建的先釋放 (一般放在try{}catch{}的finally{})if(rs!=null){ rs.close(); } if(pstmt!=null){ pstmt.close(); } if(conn!=null){ conn.close(); }
額外知識(shí)點(diǎn)
三層架構(gòu)
1. 數(shù)據(jù)訪問層 (jdbc) bean
dao
2. 業(yè)務(wù)邏輯處理層 service(面向接口編程)
3. 視圖層 web
|
M 數(shù)據(jù)
V C
視圖 控制器
封裝的源碼
public class ConnectionFactory {
private static String driver;
private static String url;
private static String user;
private static String password;
static {
driver = "com.mysql.jdbc.Driver";
url = "jdbc:mysql://127.0.0.1:3306/tbl_student";
user = "root";
password = "13870775439z";
// 從文件系統(tǒng)中獲取參數(shù)
}
/**
* 獲取連接
* */
public static Connection getConnection() throws Exception {
Class.forName(driver);
return DriverManager.getConnection(url, user, password);
}
/**
* 釋放資源
* */
public static void close(ResultSet rs, PreparedStatement pstmt,
Connection conn) throws SQLException {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
}
/*
* 插入和更新
*/
public static int Operator(String sql) {
int num = 0;
try {
Connection connection = null;
PreparedStatement pstmt = null;
try {
connection = ConnectionFactory.getConnection();
pstmt = connection.prepareStatement(sql);
num = pstmt.executeUpdate();
} finally {
ConnectionFactory.close(null, pstmt, connection);
}
} catch (Exception e) {
e.printStackTrace();
}
return num;
}
/*
* 查詢
*/
public static String queryOperator(String sql符衔,String username) {
String result="查詢失敗";
try {
ResultSet rs = null;
PreparedStatement pstmt = null;
Connection conn = null;
try {
conn = ConnectionFactory.getConnection();
pstmt = conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while (rs.next()) {
String name = rs.getString("name");
if (usernamae.equals(name)) {
result="查詢成功";
break;
}
}
} finally {
ConnectionFactory.close(rs, pstmt, conn);
}
} catch (Exception e) {
// TODO: handle exception
}
return result;
}
}