JDBC典型用法
#01 JDBC常用的接口和類
DriverManger
常用的方法
createConnetion
Connection
常用的方法
創建Statement對象
createStatement
prepareStatement
prepareCall
控制事務的方法
Statement
常用的方法
executeQuery(String sql)
executeUpdate(String sql)
execute(String sql) 執行的是Query返回的是true,執行的是Update返回的是false
子類
prepareStatement
常用的方法:
setXxx(int parameterIndex,Xxx value)
CallableStatement
ResultSet
常用的方法
void close(),
boolean absolute(int row),
void beforeFirst(),
boolean first(),
boolean previouse()
boolean next()
boolean last()
void afterLast()
getXxx(int columnIndex) 根據索引獲取值
getXxx(String columnLabel) 根據列名獲取值
#02 JDBC編程步驟
package JDBCTest;
import java.sql.*;
import java.util.Properties;
public class JDBCStep {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//加載驅動,需要處理ClassNoteFoundException
Class.forName("com.mysql.jdbc.Driver");
//創建連接,需要處理SQLException,url的規則爲:jdbc:subprotocol:other stuff,具體參見數據庫JDBC驅動文檔
//通過url,username,password獲取數據庫連接
Connection conn1 = DriverManager.getConnection("","","");
//將屬性以JDBC參數的形式拼接在url後面
Connection conn2= DriverManager.getConnection("");
//將屬性寫在properties文件中
Connection conn3 = DriverManager.getConnection("", new Properties());
//通過Connection對象創建Statement對象
Statement state1 = conn1.createStatement();
Statement state2 =conn1.prepareStatement("");
Statement state3 =conn1.prepareCall("");
//使用Statement執行SQL語句,所有的Statement都有一下三個方法
//可以執行任何sql語句,但是不推薦使用,Update返回false,Query返回true
boolean execute = state1.execute("");
//執行DML返回影響的行數 ,DDL返回結果爲0
int i = state1.executeUpdate("");
//執行查詢語句,返回代表查詢結果的ResultSet結果集
ResultSet resultSet = state1.executeQuery("");
//操縱結果集
//移動指針的方法close(),absolute(int row),beforeFirst(),first(),previouse(),next(),last(),afterLast()
//獲取記錄指針指向行,特定列的值 getXxx(int columnIndex) 根據索引獲取值,getXxx(String columnLabel) 根據列名獲取值
//回收數據庫資源,關閉ResultSet,Statement,Connection等資源
}
}
#03 執行SQL語句的方式
使用executeupdate() 7/executeLargeUpdate() 8
使用execute()
package JDBCTest;
import java.sql.*;
public class TestExecuteMethod {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("", "", "");
Statement state = conn.createStatement();
boolean hasResultSet = state.execute("");
if(hasResultSet){
//返回的結果是ResultSet
ResultSet rs = state.getResultSet();
//ResultSetMetaData是用於分析結果集的元數據接口
ResultSetMetaData rsmd = rs.getMetaData();
//獲取總共的列數
int count = rsmd.getColumnCount();
//通過ResultSet的next()方法獲取所有的值
while(rs.next()){
//依次輸出每列的值
for(int i=1;i<count;i++){
//除了Bolo類型,都可以用getString()獲取屬性值
System.out.println(rs.getString(i));
}
}
}else{
System.out.println("該SQL語句影響的記錄有+"+state.getUpdateCount()+"行");
}
}
}
使用PreparedStatement執行SQL語句
package JDBCTest;
import java.sql.*;
public class TestPreparedStatementMethod {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("", "", "");
PreparedStatement pstmt = conn.prepareStatement("insert into test(?,?,?)");
pstmt.setString(1,"");
pstmt.setString(2,"");
pstmt.setString(3,"");
int i = pstmt.executeUpdate();
PreparedStatement pstmt1 = conn.prepareStatement("select * from test where id=? and date=?");
pstmt1.setString(1,"");
pstmt1.setString(2,"");
ResultSet resultSet = pstmt1.executeQuery();
}
}
優勢
預編譯SQL,批量執行sql的時候,性能更好
無需拼接SQL語句,編程更簡單
防止SQL注入,安全性更好
注意:佔位符參數只能代替普通值,不能代替表名,列名等數據庫對象,更不能代替insert,delete的等關鍵字
使用CallableStatement調用存儲過程
#04 管理結果集(ResultSet)
可滾動,可更新的結果集
處理Blob類型數據
Blob是二進制長對象,用於存儲大文件,例如一張圖片或者一個聲音文件,將Blob數據插入數據庫需要使用PreparedStatement的setBinaryStream(int parameterIndex,InputStream x),爲指定參數傳入二進制輸入流,將Blob數據保存到數據庫。
pstmt.setBinaryStream(1, new InputStream() {
@Override
public int read() throws IOException {
return 0;
}
});
使用ResultSetMetaData分析結果集
//返回的結果是ResultSet
ResultSet rs = state.getResultSet();
//ResultSetMetaData是用於分析結果集的元數據接口
ResultSetMetaData rsmd = rs.getMetaData();
//獲取總共的列數
int count = rsmd.getColumnCount();
//獲取列索引對應的列名
String columnName = rsmd.getColumnName(1);
//獲取列索引對應的屬性類型
int columnType = rsmd.getColumnType(1);