中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

建設(shè)銀行etc信用卡申請(qǐng)網(wǎng)站google官網(wǎng)下載安裝

建設(shè)銀行etc信用卡申請(qǐng)網(wǎng)站,google官網(wǎng)下載安裝,.net做的網(wǎng)站,河北省做網(wǎng)站的企業(yè)前言 其實(shí)學(xué)Mybatis前就該學(xué)了,但是尋思目前主流框架都是用mybatis和mybatis-plus就沒再去看,結(jié)果在代碼審計(jì)中遇到了很多cms是使用jdbc的因此還是再學(xué)一下吧。 第一個(gè)JDBC程序 sql文件 INSERT INTO users(id, NAME, PASSWORD, email, birthday) VAL…

前言

其實(shí)學(xué)Mybatis前就該學(xué)了,但是尋思目前主流框架都是用mybatis和mybatis-plus就沒再去看,結(jié)果在代碼審計(jì)中遇到了很多cms是使用jdbc的因此還是再學(xué)一下吧。

第一個(gè)JDBC程序

sql文件

INSERT INTO `users`(`id`, `NAME`, `PASSWORD`, `email`, `birthday`) VALUES (1, 'zhansan', '123456', 'zs@sina.com', '1980-12-04');
INSERT INTO `users`(`id`, `NAME`, `PASSWORD`, `email`, `birthday`) VALUES (2, 'lisi', '123456', 'lisi@sina.com', '1981-12-04');
INSERT INTO `users`(`id`, `NAME`, `PASSWORD`, `email`, `birthday`) VALUES (3, 'wangwu', '123456', 'wangwu@sina.com', '1979-12-04');

HelloJDBC

import java.sql.*;public class HelloJDBC {public static void main(String[] args) throws ClassNotFoundException, SQLException {//1. 加載驅(qū)動(dòng)Class.forName("com.mysql.jdbc.Driver");//2. 用戶信息和urlString url = "jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf8&useSSL=true";String name = "root";String password = "123456";//3. 連接數(shù)據(jù)庫 connection是數(shù)據(jù)庫對(duì)象Connection connection = DriverManager.getConnection(url, name, password);//4. 執(zhí)行sql的對(duì)象 statement是執(zhí)行sql的對(duì)象Statement statement = connection.createStatement();//5. 用statement對(duì)象執(zhí)行sql語句String sql = "select * from users";ResultSet resultSet = statement.executeQuery(sql);while (resultSet.next()){System.out.println("id:"+resultSet.getObject("id")+",name:"+resultSet.getObject("name")+",password:"+resultSet.getObject("password"));System.out.println("=============================");}//6.釋放資源resultSet.close();statement.close();connection.close();}
}

Statement對(duì)象

工具類

package utils;import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;public class JdbcUtils {public static Connection connection() throws ClassNotFoundException, SQLException, IOException {InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");Properties properties = new Properties();properties.load(in);String driver = properties.getProperty("driver");String url = properties.getProperty("url");String username = properties.getProperty("username");String password = properties.getProperty("password");Class.forName(driver);return DriverManager.getConnection(url,username,password);}public static void relese(ResultSet resultSet, Statement statement, Connection connection) throws SQLException {if (resultSet!=null){resultSet.close();}if (statement!=null){statement.close();}if (connection!=null){connection.close();}}
}

Demo

import utils.JdbcUtils;import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class JdbcStatement {public static void main(String[] args) throws Exception {
//        query();insert();}public static void query() throws SQLException, ClassNotFoundException, IOException{Connection connection = JdbcUtils.connection();String sql = "select * from users";Statement statement = connection.createStatement();ResultSet resultSet = statement.executeQuery(sql);while(resultSet.next()){System.out.println("id:"+resultSet.getObject("id"));}JdbcUtils.relese(resultSet,statement,connection);}public static void insert() throws Exception{Connection connection = JdbcUtils.connection();String sql = "insert into users values(4,'Sentiment',123456,'Sentiment@qq.com','1980-12-04')";Statement statement = connection.createStatement();int i = statement.executeUpdate(sql);System.out.println(i);JdbcUtils.relese(null,statement,connection);}
}

查詢用executeQuery(),增、刪、改用executeUpdate()

PreparedStatement對(duì)象

用statement的話會(huì)有sql注入問題,因此可以用preparedstatement進(jìn)行預(yù)處理來進(jìn)行防御

主要是通過占位符來執(zhí)行查詢語句

public class JdbcPreparedStatement {public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {Connection connection = JdbcUtils.connection();String sql = "insert into users values(?,?,?,null,null)";PreparedStatement ps = connection.prepareStatement(sql);ps.setInt(1,5);ps.setString(2,"Sentiment");ps.setInt(3,123456);ps.execute();}
}

操作事務(wù)

mybatis中學(xué)過,不過連含義都忘了。。。。

其實(shí)就是執(zhí)行語句時(shí)要不都成功執(zhí)行,一個(gè)不能執(zhí)行則全部都不執(zhí)行

主要就是關(guān)閉自動(dòng)提交事務(wù)

connection.setAutoCommit(false); //開啟事務(wù)

Demo

  PreparedStatement ps = null;Connection connection = null;try {connection = JdbcUtils.connection();//關(guān)閉數(shù)煙庫的自動(dòng)提交,自動(dòng)會(huì)開啟事務(wù)connectionconnection.setAutoCommit(false); //開啟事務(wù)String sql1 = "update users set name = 'Sentiment' where id=3";ps = connection.prepareStatement(sql1);ps.executeUpdate();int x = 1 / 0;String sql2 = "update users set name = 'Sentiment' where id=1";ps = connection.prepareStatement(sql2);ps.executeUpdate();connection.commit();System.out.println("Success!");}catch (SQLException e){connection.rollback();      //執(zhí)行失敗后,事務(wù)回滾}finally {JdbcUtils.relese(null,ps,connection);}}

數(shù)據(jù)庫連接池

在上述工具類中,是通過以下方法來獲取配置文件參數(shù),并連接連接池

String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
String username = properties.getProperty("username");
String password = properties.getProperty("password");
Class.forName(driver);
return DriverManager.getConnection(url,username,password);

而我們可以用開源的數(shù)據(jù)庫連接池如DBCP、C3P0、Druid等

使用了這些數(shù)據(jù)庫連接池之后,我們?cè)陧?xiàng)目開發(fā)中就不需要編寫連接數(shù)據(jù)庫的代碼了!

DBCP

<dependency><groupId>commons-dbcp</groupId><artifactId>commons-dbcp</artifactId><version>1.4</version>
</dependency><dependency><groupId>commons-pool</groupId><artifactId>commons-pool</artifactId><version>1.5.4</version>
</dependency>

配置文件

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf8&useSSL=true
username=root
password=123456#<!-- 初始化連接 -->
initialSize=10#最大連接數(shù)量
maxActive=50#<!-- 最大空閑連接 -->
maxIdle=20#<!-- 最小空閑連接 -->
minIdle=5#<!-- 超時(shí)等待時(shí)間以毫秒為單位 6000毫秒/1000等于60-->
maxWait=60000
#JDBC驅(qū)動(dòng)建立連接時(shí)附帶的連接屬性屬性的格式必須為這樣:【屬性名=property;】
#注意:"user""password" 兩個(gè)屬性會(huì)被明確地傳遞,因此這里不需要包含他們。
connectionProperties=useUnicode=true;characterEncoding=utf8#指定由連接池所創(chuàng)建的連接的自動(dòng)提交(auto-commit)狀態(tài)。
defaultAutoCommit=true#driver default 指定由連接池所創(chuàng)建的連接的只讀(read-only)狀態(tài)。
#如果沒有設(shè)置該值,則“setReadOnly”方法將不被調(diào)用。(某些驅(qū)動(dòng)并不支持只讀模式,如:Informix)
defaultReadOnly=true#driver default 指定由連接池所創(chuàng)建的連接的事務(wù)級(jí)別(TransactionIsolation)。
#可用值為下列之一:(詳情可見javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation=READ_COMMITTED

Demo

使用DBCP后,utils就可以簡化為:

public static Connection connection() throws Exception {InputStream in = DBCP_Utils.class.getClassLoader().getResourceAsStream("dbcp.properties");Properties properties = new Properties();properties.load(in);//創(chuàng)建數(shù)據(jù)源DataSource dataSource = BasicDataSourceFactory.createDataSource(properties);return dataSource.getConnection();
}

讀取配置文件,交給數(shù)據(jù)源即可

C3P0

這個(gè)更簡單

<dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.5</version>
</dependency>
<dependency><groupId>com.mchange</groupId><artifactId>mchange-commons-java</artifactId><version>0.2.19</version>
</dependency>

配置文件

這里設(shè)置了兩個(gè)數(shù)據(jù)源:

<default-config>:默認(rèn)值創(chuàng)建數(shù)據(jù)源時(shí)不需要形參,ComboPooledDataSource ds=new ComboPooledDataSource();

<named-config name="MySQL">: 非默認(rèn)要指定數(shù)據(jù)源,ComboPooledDataSource ds=new ComboPooledDataSource(“MySQL”);

<?xml version="1.0" encoding="UTF-8"?><c3p0-config><!--c3p0的缺省(默認(rèn))配置如果在代碼中"ComboPooledDataSource ds=new ComboPooledDataSource();"這樣寫就表示使用的是c3p0的缺省(默認(rèn))--><default-config><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=true</property><property name="user">root</property><property name="password">123456</property><property name="acquiredIncrement">5</property><property name="initialPoolSize">10</property><property name="minPoolSize">5</property><property name="maxPoolSize">20</property></default-config><!--c3p0的命名配置如果在代碼中"ComboPooledDataSource ds=new ComboPooledDataSource("MySQL");"這樣寫就表示使用的是mysql的缺省(默認(rèn))--><named-config name="MySQL"><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=true</property><property name="user">root</property><property name="password">123456</property><property name="acquiredIncrement">5</property><property name="initialPoolSize">10</property><property name="minPoolSize">5</property><property name="maxPoolSize">20</property></named-config></c3p0-config>

Demo

xml文件默認(rèn)能讀取到

public static Connection connection() throws Exception {ComboPooledDataSource dataSource=new ComboPooledDataSource();return dataSource.getConnection();
}

自帶日志

在這里插入圖片描述

http://www.risenshineclean.com/news/3492.html

相關(guān)文章:

  • 成都小程序開發(fā)外包公司廣告開戶南京seo
  • 超市型網(wǎng)站開發(fā)企業(yè)微信scrm
  • 做網(wǎng)站廊坊百度網(wǎng)盤app下載安裝
  • 網(wǎng)站備案許可證號(hào)查詢b2b多平臺(tái)一鍵發(fā)布
  • 企業(yè)網(wǎng)站建設(shè)公司選擇分析免費(fèi)大數(shù)據(jù)查詢
  • 服裝企業(yè)網(wǎng)站建設(shè)現(xiàn)狀微博付費(fèi)推廣有用嗎
  • 長春做網(wǎng)站企業(yè)重慶網(wǎng)站seo好不好
  • 提供企業(yè)網(wǎng)站建設(shè)怎樣才能在百度上發(fā)布信息
  • 江西九江網(wǎng)站建設(shè)營銷型網(wǎng)站制作
  • 大鵬附近網(wǎng)站建設(shè)seo關(guān)鍵詞優(yōu)化軟件app
  • 做網(wǎng)站首頁的軟件百度一下網(wǎng)頁
  • 外文網(wǎng)站建設(shè)完成如何自建網(wǎng)站?
  • 合肥大型網(wǎng)站制作公司百度賬號(hào)官網(wǎng)
  • 網(wǎng)站做輪播圖的意義深圳網(wǎng)頁設(shè)計(jì)公司
  • 網(wǎng)站開發(fā)工程師中級(jí)高級(jí)星沙網(wǎng)站優(yōu)化seo
  • 參與賭博網(wǎng)站建設(shè)可判幾年微信推廣鏈接怎么制作
  • 微信商城網(wǎng)站建設(shè)app推廣策略
  • 北京外包公司 網(wǎng)站開發(fā)自助建站系統(tǒng)模板
  • 建設(shè)銀行網(wǎng)站功能介紹百度的seo關(guān)鍵詞優(yōu)化怎么弄
  • 陜西網(wǎng)站建設(shè)公司哪有網(wǎng)絡(luò)推廣都是收費(fèi)
  • 網(wǎng)絡(luò)優(yōu)化崗位詳細(xì)介紹網(wǎng)絡(luò)優(yōu)化工程師騙局
  • 化妝品網(wǎng)站開發(fā)的背景微信做單30元一單
  • 網(wǎng)站開發(fā)的前后端是哪些搜索引擎營銷的常見方式
  • 廣州微信網(wǎng)站建設(shè)哪家好經(jīng)典軟文案例或軟文案例
  • 美工素材網(wǎng)站有哪些百度北京總部電話
  • 長泰縣建設(shè)局網(wǎng)站電腦培訓(xùn)課程
  • 做網(wǎng)站需要Excel表格嗎國際新聞 軍事
  • 網(wǎng)站改版的宣傳詞湖南網(wǎng)絡(luò)優(yōu)化服務(wù)
  • wordpress做登陸頁面模板林哥seo
  • 做高仿表網(wǎng)站免費(fèi)seo公司