0x00前言
在一些web開發或者是資料存盤的時候,肯定會使用到資料庫來進行資料存盤,而在Java里面需要呼叫JDBC來對資料庫進行操作,每次用jdbc很麻煩,就可以采用一些連接池來解決這個問題
0x01常用類和方法
0x1Connection介面
1.createStatement()
創建一個 Statement物件,用于將SQL陳述句發送到資料庫,
2.close()
Connection發布此 Connection物件的資料庫和JDBC資源,而不是等待它們自動釋放,
3.CallableStatement prepareCall(String sql)
創建一個呼叫資料庫存盤程序的 CallableStatement物件,
4.prepareStatement(String sql)
創建一個 PreparedStatement物件,用于將引數化的SQL陳述句發送到資料庫, 對sql陳述句進行預處理,解決sql注入問題
5.boolen execute(String sql)
執行的select陳述句就回傳true,其他回傳false
6.ResultSet executeQuery(String sql)
執行給定的(select)SQL陳述句,該陳述句回傳單個 ResultSet物件,
7.int executeUpdate(String sql)
執行給定的SQL陳述句,這可能是 INSERT , UPDATE ,或 DELETE陳述句,或者不回傳任何內容,如SQL DDL陳述句的SQL陳述句,回傳的int是影響的函式
8.批處理的執行
(1)void addBatch(String sql)
將給定的SQL命令添加到此 Statement物件的當前命令串列中,
(2)int[] executeBatch()
將一批命令提交到資料庫以執行,并且所有命令都執行成功,回傳一個更新計數的陣列,
(3)void clearBatch()
清空此 Statement物件的當前SQL命令串列,
0x2ResultSet
1.表示資料庫結果集的資料表,通常通過執行查詢資料庫的陳述句生成,
2.boolean next()是否還有下一行資料存在回傳true
3.getString(String key);查找指定的列名的資料
public class JVAV_Test01 {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", "zhonglin");
//執行sql陳述句的物件
Statement statement = connection.createStatement();
String s=("select * from web_1.student;");
ResultSet resultSet = statement.executeQuery(s);
while (resultSet.next()){
System.out.println(resultSet.getString("name"));
System.out.println(resultSet.getString("id"));
}
statement.close();
resultSet.close();
}
}
0x02抽取工具類和用組態檔使用
0x1Properties類
在使用jdbc的時候會存在很多重復利用的代碼我們就可以把重復的代碼給寫成一個類供我們使用,同時使用組態檔去靈活的配置資料庫檔案,
0x2常用方法
1.String getProperty(String key)
使用此屬性串列中指定的鍵搜索屬性
2.void list(PrintStream out)
將此屬性串列列印到指定的輸出流,
public class Jdbc_toll_class {
private static final String drvierClassname;
private static final String user;
private static final String password;
private static final String url;
static {
Properties properties=new Properties();//專門處理組態檔的一個類
try {
properties.load(new FileInputStream("C:\\Users\\**\\IdeaProjects\\sec_Reptile\\src\\main\\resources\\db.properties"));//讀取組態檔路徑
} catch (IOException e) {
e.printStackTrace();
}
drvierClassname=properties.getProperty("drvierClassname");
user=properties.getProperty("user");
password=properties.getProperty("password");;
url=properties.getProperty("url");;
}
public static void loadDriver(){
try {
Class.forName(drvierClassname);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnecton() {
Connection connection = null;
try {
loadDriver();
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}return connection;
}
public static void release(Statement statement,Connection connection){
try {
connection.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void release(ResultSet resultSet, Statement statement, Connection connection){
try {
resultSet.close();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
0x03public interface PreparedStatement
0x1方法基礎使用
1.int executeUpdate()
執行在該SQL陳述句PreparedStatement物件,它必須是一個SQL資料操縱語言(DML)陳述句,如INSERT , UPDATE或DELETE ; 或不回傳任何內容的SQL陳述句,例如DDL陳述句,
2.boolean execute()
執行此 PreparedStatement物件中的SQL陳述句,這可能是任何型別的SQL陳述句,
3.ResultSet executeQuery()
執行此 PreparedStatement物件中的SQL查詢,并回傳查詢 PreparedStatement的 ResultSet物件,
4.void setInt(String,byte)(int parameterIndex, int x)
將指定的引數設定為給定的Java int(String,byte)值,
public class JAVA_test03 {
public static void main(String[] args) {
Connection connection;
PreparedStatement preparedStatement;
connection=Jdbc_toll_class.getConnection();
String sql="insert into student values (null ,?)";
try {
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,"hellow");
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
0x04批處理
1.首先要在的資料庫后面加上:?rewriteBatchedStatements=true
2.PreparedStatement的批處理是利用設定引數和循壞去處理
3.如果批處理命令過多要執行后清除再添加
public class JAVA_tese06 {
//批處理后面要加需要在連接的資料庫后面加上:?rewriteBatchedStatements=true
public static void main(String[] args) {
Connection connection = Jdbc_toll_class.getConnection();
PreparedStatement preparedStatement=null;
Jdbc_toll_class.executesql("use tese");
String sql="insert into user values(null,?)";
try {
preparedStatement = connection.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
try {
for (int i = 0; i < 10; i++) {
preparedStatement.setString(1,"試試");
preparedStatement.addBatch();
if (i %10==0){
preparedStatement.execute();
preparedStatement.clearBatch();
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
Jdbc_toll_class.release(connection,preparedStatement);
}
}
}
4.Statement批處理
5.是用addbatch,方法去把批處理命令都添加進去,用executeBatch執行
public class JAVA_test05 {
public static void main(String[] args) {
Connection connection = Jdbc_toll_class.getConnection();
try {
Statement statement = connection.createStatement();
String sql2="use tese";
String sql4="insert into user values(null,'aaa')";
String sql5="insert into user values(null,'bbb')";
String sql6="insert into user values(null,'ccc')";
String sql7="update user set name='mmm' where id=2";
String sql8="delete from user where id=1";
statement.addBatch(sql2);
statement.addBatch(sql4);
statement.addBatch(sql5);
statement.addBatch(sql6);
statement.addBatch(sql7);
statement.addBatch(sql8);
statement.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
0x05連接池
0x1c3p0連接池
public class c3p0_test {
1.獲取物件2.還是通過連接物件connection去獲取prepareStatement等物件去執行3.它的組態檔需要放在src下面,
public static void main(String[] args) throws SQLException {
DataSource dataSource=new ComboPooledDataSource();//這里可以選擇你的組態檔里面預先設定好的配置
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
0x2druid連接池
1.獲取物件是通過Properties類獲取檔案
2.InputStream resourceAsStream = Druid_test.class.getClassLoader().getResourceAsStream("druid.properties");
3.properties.load(resourceAsStream);//構建組態檔
public class Druid_test {
public static void main(String[] args) {
Properties properties=new Properties();
InputStream resourceAsStream = Druid_test.class.getClassLoader().getResourceAsStream("druid.properties");
try {
properties.load(resourceAsStream);
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
Connection connection = dataSource.getConnection();
System.out.println(connection);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.工具類的定義
1.封裝一些方法方便我們使用方法
public class JDBCUtils {
//1定義成員變數
private static DataSource dataSource;
static {
try {
Properties properties=new Properties();
properties.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
dataSource= DruidDataSourceFactory.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}public static Connection getconnection() throws SQLException {
return dataSource.getConnection();
}
public static void close(Statement statement,Connection connection){
if (statement!=null){
try {
statement.close();
}catch (Exception E){
E.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet resultSet, Statement statement, Connection connection){
if (statement!=null){
try {
statement.close();
}catch (Exception E){
E.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static DataSource getDataSource(){
return dataSource;
}
}
0x3JDBCtemplate
1.用Spring框架集成的這個JDBCtemplate,只需要獲取一個Source物件就可以直接執行sql陳述句
2,它會自己獲取連接物件和釋放連接物件,
public class JDBCtemplate {
public static void main(String[] args) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());
String sql="insert into user values (null,?)";
int i = jdbcTemplate.update(sql, "小天");
System.out.println(1);
}
0x06總結
總結下來其實Spring框架的JDBCtemplate使用起來最簡單,以后在有框架開發的時候會用到這些,開發的時候多使用 preparedStatement,預防,sql注入,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/468725.html
標籤:Java
上一篇:CAS入門實戰(1)--簡介
下一篇:maven生命周期詳解說明
