perl mysql dml_MySQL Connector执行SQL语句的三种方式
描述
當我們需要在Java程序中與數據庫進行交互,可能首先想到的是使用某個ORM框架,因為ORM框架封裝了一些實現細節,在使用上非常方便,并且一定程度上可以提升代碼穩定性。
在ORM框架中,都會依賴MySQL Connector包,因為真正與數據庫進行交互的是在MySQL Connector包里面實現。
5.x版本maven依賴:
mysql
mysql-connector-java
5.1.47
三種方式
MySQL Connector執行SQL語句主要有executeQuery,executeUpdate,execute等三種方式。
executeQuery
此方法執行Selec查詢語句,通過ResultSet返回結果集。
private static void executeQuery() throws Exception{
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try {
connection = DriverManager.getConnection(url);
statement = connection.createStatement();
statement.setFetchSize(Integer.MIN_VALUE);
String sql = "select * from user";
rs = statement.executeQuery(sql);
int count=0;
while (rs.next()){
count++;
}
System.out.println("executeQuery count: " + count);
} catch (Exception e){
e.printStackTrace();
} finally {
close(connection,statement,rs);
}
}
executeUpdate
此方法執行Insert,Update,Delete語句,返回變更影響的行數。
private static void executeUpdate() throws Exception{
Connection connection = null;
Statement statement = null;
int updateCount = 0;
try {
connection = DriverManager.getConnection(url);
statement = connection.createStatement();
String sql = "update user set name='啊啊啊' where id = " + new Random().nextInt(999999);
updateCount = statement.executeUpdate(sql);
System.out.println("executeUpdate count: " + updateCount);
} catch (Exception e){
e.printStackTrace();
} finally {
close(connection,statement,null);
}
}
execute
當我們不知道來源SQL是Select查詢還是Insert/Update/Delete更新時,可以統一使用excute()方法來執行SQL語句,此方法返回一個boolean值,如果返回true,表示執行的SQL語句為Select查詢語句,此時可以通過Statement#getResultSet()方法來獲取結果集;如果返回false,表示執行的時Insert/Update/Delete語句,此時可以通過Statement#getUpdateCount()來返回此次SQL執行對數據庫影響的行數。
private static void executeForSelect() throws Exception{
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try {
connection = DriverManager.getConnection(url);
statement = connection.createStatement();
statement.setFetchSize(Integer.MIN_VALUE);
String sql = "select * from user";
if (statement.execute(sql)){
rs = statement.getResultSet();
}
int count=0;
while (rs.next()){
count++;
}
System.out.println("executeForSelect count: " + count);
} catch (Exception e){
e.printStackTrace();
} finally {
close(connection,statement,rs);
}
}
private static void executeForUpdate() throws Exception{
Connection connection = null;
Statement statement = null;
int updateCount = 0;
try {
connection = DriverManager.getConnection(url);
statement = connection.createStatement();
String sql = "update user set name='啊啊啊' where id = " + new Random().nextInt(999999);
if (!statement.execute(sql)){
updateCount = statement.getUpdateCount();
}
System.out.println("executeForUpdate updateCount: " + updateCount);
} catch (Exception e){
e.printStackTrace();
} finally {
close(connection,statement,null);
}
}
驗證
執行測試:
public static void main(String[] args) throws Exception {
Long start = System.currentTimeMillis();
executeForSelect();
System.out.println("executeForSelect 耗時: " + (System.currentTimeMillis() - start) + " ms \n");
start = System.currentTimeMillis();
executeForUpdate();
System.out.println("executeForUpdate 耗時: " + (System.currentTimeMillis() - start) + " ms \n");
start = System.currentTimeMillis();
executeQuery();
System.out.println("executeQuery 耗時: " + (System.currentTimeMillis() - start) + " ms \n");
start = System.currentTimeMillis();
executeUpdate();
System.out.println("executeUpdate 耗時: " + (System.currentTimeMillis() - start) + " ms \n");
}
private static void close(Connection connection, Statement statement, ResultSet rs) throws Exception{
if (rs != null){
rs.close();
}
if (statement != null){
statement.close();
}
if (connection != null){
connection.close();
}
}
返回結果:
executeForSelect count: 4717924
executeForSelect 耗時: 4791 ms
executeForUpdate updateCount: 1
executeForUpdate 耗時: 6 ms
executeQuery count: 4717924
executeQuery 耗時: 4340 ms
executeUpdate count: 1
executeUpdate 耗時: 8 ms
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的perl mysql dml_MySQL Connector执行SQL语句的三种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android设置大小能用小数,Andr
- 下一篇: php输出mysql的数据结构_php课