原文鏈接http://zhhll.icu/2020/11/28/java%E5%9F%BA%E7%A1%80/JDBC/JDBC%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C/
JDBC基本操作
create table user(
id int primary key auto_increment,
name varchar(50)
) ENGINE = InnoDB DEFAULT CHARSET = utf8;
JDBC概念
JDBC是一個獨立于特定資料庫管理系統、通用的SQL資料庫存取和操作的公共介面,定義了用來訪問資料庫的標準的Java類別庫
連接步驟
-
加載驅動
-
進行資料庫連接
// 驅動
private static final String DRIVER = "com.mysql.jdbc.Driver";
// 地址
private static final String URL = "jdbc:mysql://localhost:3306/test";
//用戶名
private static final String USER_NAME = "root";
// 密碼
private static final String PSW = "123456";
/**
* 獲取連接
*/
public static Connection getConnection(){
Connection conn = null;
try {
// 加載驅動
Class.forName(DRIVER);
// 資料庫連接
conn = DriverManager.getConnection(URL,USER_NAME,PSW);
} catch (ClassNotFoundException e) {
System.out.println("加載驅動失敗,請檢查是否引入Jar包或者驅動名稱是否正確");
throw new RuntimeException("加載驅動失敗,請檢查是否引入Jar包或者驅動名稱是否正確",e);
} catch (SQLException throwables) {
System.out.println("連接資料庫失敗,請檢查資料庫地址,用戶名,密碼是否正確");
throw new RuntimeException("連接資料庫失敗,請檢查資料庫地址,用戶名,密碼是否正確",throwables);
}
return conn;
}
/**
* 關閉連接
* @param conn
*/
public static void close(Connection conn){
if(conn != null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
注意:為什么需要使用Class.forName()來加載資料庫驅動
是因為在每個Driver中都包含有一個靜態代碼塊,實際呼叫的是DriverManager.registerDriver(new Driver());方法
public class Driver extends NonRegisteringDriver implements java.sql.Driver {
public Driver() throws SQLException {
}
static {
try {
DriverManager.registerDriver(new Driver());
} catch (SQLException var1) {
throw new RuntimeException("Can't register driver!");
}
}
}
DriverManager
該類進行資料庫驅動的管理,可以注冊多個資料庫驅動,根據url來動態的選擇不同的資料庫連接,
操作資料庫
Statement介面
使用Statement介面來操作靜態的SQL陳述句
executeUpdate方法
添加
/**
* 插入操作
* @param sql
*/
public static void doInsert(String sql){
Connection conn = getConnection();
Statement statement = null;
try {
statement = conn.createStatement();
int result = statement.executeUpdate(sql);
System.out.println(sql+"執行成功,插入"+result+"條資料");
} catch (SQLException e) {
throw new RuntimeException("執行失敗",e);
} finally {
if(statement != null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
close(conn);
}
}
修改
/**
* 修改操作
* @param sql
*/
public static void doUpdate(String sql){
Connection conn = getConnection();
Statement statement = null;
try {
statement = conn.createStatement();
int result = statement.executeUpdate(sql);
System.out.println(sql+"執行成功,修改"+result+"條資料");
} catch (SQLException e) {
throw new RuntimeException("執行失敗",e);
} finally {
if(statement != null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
close(conn);
}
}
洗掉
/**
* 洗掉操作
* @param sql
*/
public static void doDelete(String sql){
Connection conn = getConnection();
Statement statement = null;
try {
statement = conn.createStatement();
int result = statement.executeUpdate(sql);
System.out.println(sql+"執行成功,洗掉"+result+"條資料");
} catch (SQLException e) {
throw new RuntimeException("執行失敗",e);
} finally {
if(statement != null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
close(conn);
}
}
PreparedStatement介面
該介面為Statement的子介面,屬于預處理操作,可以傳入帶有占位符的SQL,然后在進行補充占位符,索引值從1開始,
可以有效地禁止SQL注入
executeUpdate方法
插入
public static void doPreparedInsert(String name){
Connection conn = getConnection();
PreparedStatement statement = null;
try {
String sql = "insert into user (name) values (?)";
statement = conn.prepareStatement(sql);
statement.setString(1,name);
int result = statement.executeUpdate();
System.out.println(sql+"執行成功,插入"+result+"條資料");
} catch (SQLException e) {
throw new RuntimeException("執行失敗",e);
} finally {
if(statement != null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
close(conn);
}
}
在創建preparedStatement物件時,有一個多載方法
// 第二個引數表示一個是否回傳自增主鍵的一個biaoshi
// Statement.RETURN_GENERATED_KEYS
// Statement.NO_GENERATED_KEYS
PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
在使用該PreparedStatement執行插入操作時,可以使用statement.getGeneratedKeys()來回傳一個新生成主鍵的ResultSet物件,結果集中只有一列GENERATED_KEY,存放的新生成的主鍵值
更新
與插入類似
洗掉
與插入類似
結果集
在查詢資料時,回傳的是一個二維的結果集,使用ResultSet來遍歷結果集
public static void doQuery(){
String sql = "select * from user";
Connection conn = getConnection();
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
statement = conn.prepareStatement(sql);
resultSet = statement.executeQuery();
// resultSet.next 方法 將游標向前移動一行,最初為第一行之前,第一次呼叫使得第一行為當前行
while (resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println("查詢到id為"+id+",name為"+name+"的記錄");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
if(resultSet != null){
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
close(conn,statement);
}
}
批量操作
public static void doBatchInsert(String sql){
Connection conn = getConnection();
PreparedStatement statement = null;
try {
statement = conn.prepareStatement(sql);
for(int i = 0;i<1000;i++){
statement.setString(1,"張三"+i);
// 積攢sql
statement.addBatch();
}
// 執行sql
statement.executeBatch();
// 清除積攢的sql
statement.clearBatch();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
close(conn,statement);
}
}
由于本身的博客百度沒有收錄,博客地址http://zhhll.icu
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/250030.html
標籤:Java
上一篇:shiro的簡介(一)
