JDBC(Mysql)個人總結
JDBC(Mysql)個人學習
1,什麼是JDBC?
2,JDBC可以做什麼?
3,如何去使用JDBC?
什麼是JDBC?
JDBC(Java DataBase Connectivity),java資料庫連線,是一種用於執行SQL語句的Java API,也可以這樣理解,是Java語言定義的一個SQL呼叫介面。方便了Java程式猿。
JDBC可以做什麼?
JDBC 可做三件事:與資料庫建立連線、傳送操作資料庫的語句並處理結果。
如何去使用JDBC?
1,註冊驅動(兩種方法)
1,
com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();
2,Class.forName("com.mysql.jdbc.Driver");
(運用了Java中的反射機制)2,獲得資料庫連結
Connection connection = DriverManager.getConnection(url,user,password);
Connection類 屬於java.sql.Connection 包下。3,創造資料庫操作物件
1,
Statement statement = connection.createStatement();
Statement類 屬於java.sql.Statement,此操作物件一般用於執行靜態sql語句
2,PreparedStatement preparedStatement = connection.prepareStatement();
PreparedStatement 繼承Statement類 屬於java.sql.Statement,此操作物件一般用於執行動態sql
語句,可以進行sql語句的預編譯,可以防止部分情況下的sql語句注入等。4,定義sql語句
1,
String sql = "select * from test"
靜態sql查詢語句
String sql = "insert into test (name,age) values ("zhangsan",20)";
靜態sql插
入語句,(可自己寫入delete,update等語句)
2,String sql = "select * from test where name = ?
動態sql查詢語句
String sql = "insert into test (name,age) values (?,?)";
其中的?代表佔位符。此語句用於preparedStatement進行sql語句的預編譯5,執行sql語句
1,查詢語句的執行
1,靜態查詢
Result result = statement.exeuteQuery(sql);
以上語句執行會得到一個結果集,方便對查詢結果的處理。
2,動態查詢
preparedStatement.setString(1,"zhangsan");
Result result = preparedStatement.exeuteQuery();
以上語句執行會得到一個結果集,方便對查詢結果的處理。2,動態語句的執行
1,靜態增刪改
int count = statement.executeUpdate(sql);
其中count為每次對資料庫正確操作後返回的一個整數。
2,動態增刪改
int count = prepareStatement.executeUpdate();
6,對結果集進行操作
如:
while(resultSet.next()){
String name = resultSet.getString("name");
String age = resultSet.getString("age");
System.out.println("name:" + name + ",age:" + age);}
7,關閉資源
close();各個物件呼叫方法關閉該資源。
以下為展示程式碼。
package cn.sxflow.JDBC;
//此為靜態查詢
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//靜態sql語句查詢
public class Test01 {
public static void main(String[] args) {
try {
//註冊驅動
com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();
//連結資料庫
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123123");
//獲取資料庫操作物件
Statement statement = connection.createStatement();
//執行sql語句,sql語句出現文件中的順序不受限制
//該例測試的是查詢語句
String sql = "select * from student";
//sql - 要傳送到資料庫的SQL語句,通常為靜態SQL SELECT語句
//結果:一個ResultSet物件,其中包含給定查詢產生的資料; 從不null
ResultSet resultSet = statement.executeQuery(sql);
//對結果集進行處理
while(resultSet.next()) {
String name = resultSet.getString("name");
String age = resultSet.getString("age");
System.out.println("name:" + name + ",age:" + age);
}
resultSet.close();
statement.close();
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package cn.sxflow.JDBC;
//此為靜態插入
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test02 {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123123");
statement = connection.createStatement();
String sql = "insert into student (name,age) values ('wangwu',30),('zhaoliu',31)";
//執行給定的SQL語句,這可能是INSERT , UPDATE ,或DELETE語句,或者不返回任何內容,如SQL DDL語句的SQL語句。
int executeUpdate = statement.executeUpdate(sql);
System.out.println(executeUpdate);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(resultSet!=null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (statement!=null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection!=null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package cn.sxflow.JDBC;
//此為動態插入
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test03 {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement prepareStatement = null;
try {
//註冊驅動
Class.forName("com.mysql.jdbc.Driver");
//資料庫連結
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123123");
String sql = "insert into student values (?,?)";
//編譯預處理
prepareStatement = connection.prepareStatement(sql);
prepareStatement.setString(1, "蠟筆小新");
prepareStatement.setInt(2, 25);
int executeUpdate = prepareStatement.executeUpdate();
System.out.println(executeUpdate);
} catch (Exception e) {
// TODO: handle exception
} finally {
if (prepareStatement != null) {
try {
prepareStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
關於事務的提交。
值得注意的是,在mysql中有事務一說。
什麼是事務?
即使用者定義的一個數據庫操作序列。這些操作要麼全部執行,要麼就是任意一個執行失敗後,其他的也全部失效,自動恢復至原樣,這種行為被稱為回滾。
關於事務的展示程式碼如下:
package cn.sxflow.JDBC;
//此為事務的手動提交
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test04 {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
String className = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "123456";
try {
Class.forName(className);
connection = DriverManager.getConnection(url, user, password);
//關閉事務的自動提交
connection.setAutoCommit(false);
String sql = "insert into student values(?,?)";
statement = connection.prepareStatement(sql);
statement.setString(1, "zhangsan");
statement.setInt(2, 30);
int executeUpdate = statement.executeUpdate();
System.out.println(executeUpdate);
//手動提交事務
connection.commit();
} catch (Exception e) {
System.out.println("操作失敗,請重試!!!");
try {
connection.rollback();
} catch (Exception e2) {
e2.getMessage();
}
} finally {
if(resultSet!= null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}