Sun公司開發一組標準api,他們只是接口,並沒有提供實現類,由數據庫廠商提供實現類,即驅動程序
jdbc操作過程:
jar包導入
定義記錄的類(如Student類)
連接的獲取
sql的執行
// 連接的獲取:
private static Connection getConn() {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/samp_db";
String username = "root";
String password = "";
Connection conn = null;
try {
Class.forName(driver); //classLoader,加載對應驅動
conn = (Connection) DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
// sql的執行
// insert
private static int insert(Student student) {
Connection conn = getConn();
int i = 0;
String sql = "insert into students (Name,Sex,Age) values(?,?,?)";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getSex());
pstmt.setString(3, student.getAge());
i = pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
// update
private static int update(Student student) {
Connection conn = getConn();
int i = 0;
String sql = "update students set Age='" + student.getAge() + "' where Name='" + student.getName() + "'";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
i = pstmt.executeUpdate();
System.out.println("resutl: " + i);
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
// select xx from tableName
private static Integer getAll() {
Connection conn = getConn();
String sql = "select * from students";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement)conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
int col = rs.getMetaData().getColumnCount();
System.out.println("============================");
while (rs.next()) {
for (int i = 1; i <= col; i++) {
System.out.print(rs.getString(i) + "\t");
if ((i == 2) && (rs.getString(i).length() < 8)) {
System.out.print("\t");
}
}
System.out.println("");
}
System.out.println("============================");
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
// delete
private static int delete(String name) {
Connection conn = getConn();
int i = 0;
String sql = "delete from students where Name='" + name + "'";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
i = pstmt.executeUpdate();
System.out.println("resutl: " + i);
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
測試:
// 測試
public static void main(String args[]) {
JDBCOperation.getAll();
JDBCOperation.insert(new Student("Achilles", "Male", "14"));
JDBCOperation.getAll();
JDBCOperation.update(new Student("Bean", "", "7"));
JDBCOperation.delete("Achilles");
JDBCOperation.getAll();
}
輸出結果:
============================
1 Ender male 8
2 Bean male 6
3 Petra fema 9
4 Peter male 9
5 _Graff male 40
6 GOD fema 255
============================
============================
1 Ender male 8
2 Bean male 6
3 Petra fema 9
4 Peter male 9
5 _Graff male 40
6 GOD fema 255
7 Achilles Male 14
============================
resutl: 1
resutl: 1
============================
1 Ender male 8
2 Bean male 7
3 Petra fema 9
4 Peter male 9
5 _Graff male 40
6 GOD fema 255
============================
代碼分析:
在上述對數據庫進行增刪改查的過程中,可以發現其共性部分,即通用的流程:
(1)創建Connection對象、SQL查詢命令字符串;
(2)對Connection對象傳入SQL查詢命令,獲得PreparedStatement對象;
(3)對PreparedStatement對象執行executeUpdate()或executeQurey()獲得結果;
(4)先後關閉PreparedStatement對象和Connection對象。
可見,使用JDBC時,最常打交道的是Connection、PreparedStatement這兩個類,以及select中的ResultSet類。
思考問題
1.每次SQL操作都需要建立和關閉連接,這勢必會消耗大量的資源開銷,如何避免?
分析:可以採用連接池,對連接進行統一維護,不必每次都建立和關閉。事實上這是很多對JDBC進行封裝的工具所採用的。
2.Java代碼中,傳入的數據格式與數據庫定義不同怎麼辦?如把Java的String對象賦值給數據庫的tinyint屬性。
分析:在執行SQL語句時,數據庫會嘗試進行轉換。根據我的實驗,如果用內容爲純字母的String對象傳入tinyint的age屬性時,會被轉化成0。具體轉化規則應該和數據庫有關。