1、在IDEA里創建Maven項目
1.1、點擊Create New Project




2、用Maven連接Mysql的JDBC驅動
2.1、打開src下的pom.xml文件, 在里面添加Mysql的jdbc包的引用,代碼如下
1 <dependencies> 2 <dependency> 3 <groupId>mysql</groupId> 4 <artifactId>mysql-connector-java</artifactId> 5 <version>8.0.18</version> 6 </dependency> 7 </dependencies>
2.2、添加完成后,IDEA右下角會出現下圖提示,點擊提示中的Import Changes,Maven就會開始下載資源

2.3、下載時頁面左下角出現正在下載的提示

2.4、下載完成變成綠勾

3、連接數據庫第一種方式:直接注冊驅動,向數據庫插入數據(不推薦使用)
3.1、在src——main——java目錄下,新建一個LinkDatabaseInsert的類
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.PreparedStatement; 4 import java.sql.SQLException; 5 6 public class LinkDatabaseInsert { 7 public static void main(String[] args) throws ClassNotFoundException, SQLException { 8 //1.注冊數據庫的驅動 9 Class.forName("com.mysql.jdbc.Driver"); 10 //2.獲取數據庫連接(里面內容依次是:"jdbc:mysql://主機名:端口號/數據庫名","用戶名","登錄密碼") 11 Connection connection = DriverManager.getConnection("jdbc:mysql://rm-uf6lgkv4fd9776rxego.mysql.rds.aliyuncs.com:3306/study","root","whmilyY123!"); 12 //3.需要執行的sql語句(?是占位符,代表一個參數) 13 String sql = "insert into stu(id,name,age) values(?,?,?)"; 14 //4.獲取預處理對象,並依次給參數賦值 15 PreparedStatement statement = connection.prepareCall(sql); 16 statement.setInt(1,12); //數據庫字段類型是int,就是setInt;1代表第一個參數 17 statement.setString(2,"小明"); //數據庫字段類型是String,就是setString;2代表第二個參數 18 statement.setInt(3,16); //數據庫字段類型是int,就是setInt;3代表第三個參數 19 //5.執行sql語句(執行了幾條記錄,就返回幾) 20 int i = statement.executeUpdate(); 21 System.out.println(i); 22 //6.關閉jdbc連接 23 statement.close(); 24 connection.close(); 25 } 26 }

3.2、運行程序,返回1,說明插入成功

4、連接數據庫第二種方式——新建數據庫配置文件,獲取配置文件信息后,再注冊數據庫驅動
4.1、利用反射獲取新建的數據庫配置文件db.properties里的信息
4.1.1、在src——main——resources目錄下,新建db.properties文件
1 driver=com.mysql.jdbc.Driver 2 url=jdbc:mysql://rm-uf6lg6rxego.mysql.rds.aliyuncs.com:3306/study 3 user=root 4 password=wY123!

4.1.2、新建util包,然后在里面創建JdbcUtil類,利用反射獲取db.properties文件信息,最后返回數據庫連接
1 package util; 2 3 import java.io.InputStream; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.util.Properties; 7 8 //獲取到db.properties文件中的數據庫信息 9 public class JdbcUtil { 10 //私有變量 11 private static String driver; 12 private static String url; 13 private static String user; 14 private static String password; 15 16 //靜態塊 17 static{ 18 try{ 19 //1.新建屬性集對象 20 Properties properties = new Properties(); 21 //2通過反射,新建字符輸入流,讀取db.properties文件 22 InputStream input = JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties"); 23 //3.將輸入流中讀取到的屬性,加載到properties屬性集對象中 24 properties.load(input); 25 //4.根據鍵,獲取properties中對應的值 26 driver = properties.getProperty("driver"); 27 url = properties.getProperty("url"); 28 user = properties.getProperty("user"); 29 password = properties.getProperty("password"); 30 }catch(Exception e){ 31 e.printStackTrace(); 32 } 33 } 34 35 //返回數據庫連接 36 public static Connection getConnection(){ 37 try{ 38 //注冊數據庫的驅動 39 Class.forName(driver); 40 //獲取數據庫連接(里面內容依次是:主機名和端口、用戶名、密碼) 41 Connection connection = DriverManager.getConnection(url,user,password); 42 //返回數據庫連接 43 return connection; 44 }catch (Exception e){ 45 e.printStackTrace(); 46 } 47 return null; 48 } 49 }

4.1.3、在java目錄下創建LinkMysql類,調用JdbcUtil類返回的數據庫連接操作數據庫
1 import util.JdbcUtil; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.SQLException; 6 7 public class LinkMysql { 8 public static void main(String[] args) throws ClassNotFoundException, SQLException { 9 //獲取數據庫連接 10 Connection connection = JdbcUtil.getConnection(); 11 //需要執行的sql語句 12 String sql = "insert into stu(id,name,age) values(?,?,?)"; 13 //獲取預處理對象,並給參數賦值 14 PreparedStatement statement = connection.prepareCall(sql); 15 statement.setInt(1,14); 16 statement.setString(2,"李四"); 17 statement.setInt(3,16); 18 //執行sql語句(插入了幾條記錄,就返回幾) 19 int i = statement.executeUpdate(); //executeUpdate:執行並更新 20 System.out.println(i); 21 //關閉jdbc連接 22 statement.close(); 23 connection.close(); 24 } 25 }

4.2、通過ResourceBundle類獲取新建的數據庫配置文件db.properties里的信息
4.2.1、在util包里面創建創建JdbcUtil2類,ResourceBundle類獲取db.properties文件信息,最后返回數據庫連接
1 package util; 2 3 import java.sql.*; 4 import java.util.ResourceBundle; 5 6 public class JdbcUtil2 { 7 //私有變量 8 private static String driver; 9 private static String url; 10 private static String user; 11 private static String password; 12 13 //靜態塊 14 static{ 15 try{ 16 //2.3通過ResourceBundle類拿到數據庫連接信息 17 ResourceBundle resourceBundle = ResourceBundle.getBundle("db"); 18 driver = resourceBundle.getString("driver"); 19 url = resourceBundle.getString("url"); 20 user = resourceBundle.getString("user"); 21 password = resourceBundle.getString("password"); 22 }catch(Exception e){ 23 e.printStackTrace(); 24 } 25 } 26 27 //返回數據庫連接 28 public static Connection getConnection(){ 29 try{ 30 //注冊數據庫的驅動 31 Class.forName(driver); 32 //獲取數據庫連接(里面內容依次是:主機名和端口、用戶名、密碼) 33 Connection connection = DriverManager.getConnection(url,user,password); 34 //返回數據庫連接 35 return connection; 36 }catch (Exception e){ 37 e.printStackTrace(); 38 } 39 return null; 40 } 41 42 //關閉結果集 43 public static void closeResultSet(ResultSet resultSet) { 44 if (resultSet != null) { 45 try { 46 resultSet.close(); 47 } catch (SQLException e) { 48 e.printStackTrace(); 49 } 50 } 51 } 52 53 //關閉預處理對象 54 public static void closeStatement(Statement statement) { 55 if (statement != null) { 56 try { 57 statement.close(); 58 } catch (SQLException e) { 59 e.printStackTrace(); 60 } 61 } 62 } 63 64 //關閉數據庫連接 65 public static void closeConnection(Connection connection){ 66 if(connection != null){ 67 try { 68 connection.close(); 69 } catch (SQLException e) { 70 e.printStackTrace(); 71 } 72 } 73 } 74 75 //一次性關閉上面三個 76 public static void closeResource(ResultSet resultSet,Statement statement,Connection connection){ 77 closeResultSet(resultSet); 78 closeStatement(statement); 79 closeConnection(connection); 80 } 81 }

4.2.2、在java目錄下創建LinkMysql2類,調用JdbcUtil2類返回的數據庫連接操作數據庫
1 import util.JdbcUtil2; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.SQLException; 6 7 public class LinkMysql2 { 8 public static void main(String[] args) throws ClassNotFoundException, SQLException { 9 //獲取數據庫連接 10 Connection connection = JdbcUtil2.getConnection(); 11 //需要執行的sql語句 12 String sql = "insert into stu(id,name,age) values(?,?,?)"; 13 //獲取預處理對象,並給參數賦值 14 PreparedStatement statement = connection.prepareCall(sql); 15 statement.setInt(1,19); 16 statement.setString(2,"王五"); 17 statement.setInt(3,16); 18 //執行sql語句(執行了幾條記錄,就返回幾) 19 int i = statement.executeUpdate(); //executeUpdate:執行並更新 20 System.out.println(i); 21 //關閉jdbc連接 22 JdbcUtil2.closeResource(null,statement,connection); 23 } 24 } 25 import util.JdbcUtil2; 26 27 import java.sql.Connection; 28 import java.sql.PreparedStatement; 29 import java.sql.SQLException; 30 31 public class LinkMysql2 { 32 public static void main(String[] args) throws ClassNotFoundException, SQLException { 33 //獲取數據庫連接 34 Connection connection = JdbcUtil2.getConnection(); 35 //需要執行的sql語句 36 String sql = "insert into stu(id,name,age) values(?,?,?)"; 37 //獲取預處理對象,並給參數賦值 38 PreparedStatement statement = connection.prepareCall(sql); 39 statement.setInt(1,19); 40 statement.setString(2,"王五"); 41 statement.setInt(3,16); 42 //執行sql語句(執行了幾條記錄,就返回幾) 43 int i = statement.executeUpdate(); //executeUpdate:執行並更新 44 System.out.println(i); 45 //關閉jdbc連接 46 JdbcUtil2.closeResource(null,statement,connection); 47 } 48 }


5、連接數據庫的第三種方式:新建數據庫配置文件,獲取配置文件信息后,再通過DButils工具包連接數據庫
1 <dependency> 2 <groupId>commons-dbutils</groupId> 3 <artifactId>commons-dbutils</artifactId> 4 <version>1.5</version> 5 </dependency>
1 import org.apache.commons.dbutils.QueryRunner; 2 import util.JdbcUtil2; 3 4 import java.sql.Connection; 5 import java.sql.SQLException; 6 7 public class DbutilsInsert { 8 public static void main(String[] args) throws SQLException { 9 //創建dbUtils里面的QueryRunner對象 10 QueryRunner queryRunner = new QueryRunner(); 11 //sql語句 12 String sql = "insert into stu(id,name,age) values(?,?,?)"; 13 //存參數值的數組 14 Object[] objects = {20,"小紅",11}; 15 //獲取數據庫連接 16 Connection connection = JdbcUtil2.getConnection(); 17 //執行sql語句,並返回影響的行數 18 int i = queryRunner.update(connection,sql,objects); 19 System.out.println(i); 20 //關閉數據庫連接 21 connection.close(); 22 } 23 }



1 import org.apache.commons.dbutils.QueryRunner; 2 import util.JdbcUtil2; 3 4 import java.sql.Connection; 5 import java.sql.SQLException; 6 7 public class DbutilsUpdate { 8 public static void main(String[] args) throws SQLException { 9 //創建dbUtils里面的QueryRunner對象 10 QueryRunner queryRunner = new QueryRunner(); 11 //sql語句 12 String sql = "update stu set name=? where id=?"; 13 //存參數值的數組 14 Object[] objects = {"紅紅",21}; 15 //獲取數據庫連接 16 Connection connection = JdbcUtil2.getConnection(); 17 //執行sql語句,並返回影響的行數 18 int i = queryRunner.update(connection,sql,objects); 19 System.out.println(i); 20 //關閉數據庫連接 21 connection.close(); 22 } 23 }

1 import org.apache.commons.dbutils.QueryRunner; 2 import util.JdbcUtil2; 3 4 import java.sql.Connection; 5 import java.sql.SQLException; 6 7 public class DbutilsDelete { 8 public static void main(String[] args) throws SQLException { 9 //創建dbUtils里面的QueryRunner對象 10 QueryRunner queryRunner = new QueryRunner(); 11 //sql語句 12 String sql = "delete from stu where id=?"; 13 //存參數值的數組 14 Object[] objects = {11}; 15 //獲取數據庫連接 16 Connection connection = JdbcUtil2.getConnection(); 17 //執行sql語句,並返回影響的行數 18 int i = queryRunner.update(connection,sql,objects); 19 System.out.println(i); 20 //關閉數據庫連接 21 connection.close(); 22 } 23 }
5.5、創建UtilsSelectArrayHandler類,查詢數據(ArrayHandler()只會返回第一條記錄)
1 import org.apache.commons.dbutils.QueryRunner; 2 import org.apache.commons.dbutils.handlers.ArrayHandler; 3 import util.JdbcUtil2; 4 5 import java.sql.Connection; 6 import java.sql.SQLException; 7 import java.util.Arrays; 8 9 public class UtilsSelectArrayHandler { 10 public static void main(String[] args) throws SQLException { 11 //創建dbUtils里面的QueryRunner對象 12 QueryRunner queryRunner = new QueryRunner(); 13 //sql語句 14 String sql = "select * from stu where age>?"; 15 //獲取數據庫連接 16 Connection connection = JdbcUtil2.getConnection(); 17 //存參數值的數組 18 Object[] params = {16}; 19 //執行查詢,並以數組的形式返回查詢結果(new ArrayHandler()只會返回第一條記錄) 20 Object[] objects = queryRunner.query(connection,sql, new ArrayHandler(),params); 21 System.out.println(Arrays.toString(objects)); 22 System.out.println("\n"); 23 //關閉數據庫連接 24 connection.close(); 25 } 26 }
5.6、創建UtilsSelectArrayListHandler類,查詢數據(ArrayListHandler()會返回所有查詢到的記錄)
1 import org.apache.commons.dbutils.QueryRunner; 2 import org.apache.commons.dbutils.handlers.ArrayListHandler; 3 import util.JdbcUtil2; 4 5 import java.sql.Connection; 6 import java.sql.SQLException; 7 import java.util.Arrays; 8 import java.util.List; 9 10 public class UtilsSelectArrayListHandler { 11 public static void main(String[] args) throws SQLException { 12 //創建dbUtils里面的QueryRunner對象 13 QueryRunner queryRunner = new QueryRunner(); 14 //sql語句 15 String sql = "select * from stu where age>?"; 16 //獲取數據庫連接 17 Connection connection = JdbcUtil2.getConnection(); 18 //存參數值的數組 19 Object[] params = {16}; 20 //執行查詢,並以數組的形式返回查詢結果(new ArrayListHandler()返回所有查詢到的記錄) 21 List<Object[]> lists = queryRunner.query(connection,sql, new ArrayListHandler(),params); 22 for(Object[] item:lists){ 23 System.out.println(Arrays.toString(item)); 24 } 25 //關閉數據庫連接 26 connection.close(); 27 } 28 }
先創建student類
1 public class Student { 2 private String id; 3 private String name; 4 private int age; 5 private String tname; 6 7 public String getId() { 8 return id; 9 } 10 11 public void setId(String id) { 12 this.id = id; 13 } 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 public int getAge() { 24 return age; 25 } 26 27 public void setAge(int age) { 28 this.age = age; 29 } 30 31 public String getTname() { 32 return tname; 33 } 34 35 public void setTname(String tname) { 36 this.tname = tname; 37 } 38 39 @Override 40 public String toString() { 41 return "Student{" + 42 "id='" + id + '\'' + 43 ", name='" + name + '\'' + 44 ", age=" + age + 45 ", tname='" + tname + '\'' + 46 '}'; 47 } 48 }
再創建UtilsSelectBeanHandler類
import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import util.JdbcUtil2; import java.sql.Connection; import java.sql.SQLException; public class UtilsSelectBeanHandler { public static void main(String[] args) throws SQLException { //創建dbUtils里面的QueryRunner對象 QueryRunner queryRunner = new QueryRunner(); //sql語句 String sql = "select * from stu where age>?"; //獲取數據庫連接 Connection connection = JdbcUtil2.getConnection(); //存參數值的數組 Object[] params = {16}; //執行查詢,並以數組的形式返回查詢結果(new BeanHandler()只會返回第一條記錄,並轉成對象) Student student = queryRunner.query(connection,sql, new BeanHandler<Student>(Student.class),params); System.out.println(student); System.out.println("\n"); //關閉數據庫連接 connection.close(); } }


1 import org.apache.commons.dbutils.QueryRunner; 2 import org.apache.commons.dbutils.handlers.BeanListHandler; 3 import util.JdbcUtil2; 4 5 import java.sql.Connection; 6 import java.sql.SQLException; 7 import java.util.List; 8 9 public class UtilsSelectBeanListHandler { 10 public static void main(String[] args) throws SQLException { 11 //創建dbUtils里面的QueryRunner對象 12 QueryRunner queryRunner = new QueryRunner(); 13 //sql語句 14 String sql = "select * from stu where age>?"; 15 //獲取數據庫連接 16 Connection connection = JdbcUtil2.getConnection(); 17 //存參數值的數組 18 Object[] params = {16}; 19 //執行查詢,並以數組的形式返回查詢結果(new BeanListHandler()返回查詢到的所有記錄,並轉成對象) 20 List<Student> students = queryRunner.query(connection,sql, new BeanListHandler<Student>(Student.class),params); 21 System.out.println(students); 22 //關閉數據庫連接 23 connection.close(); 24 } 25 }
5.9、創建UtilsSelectColumnListHandler類,查詢數據(ColumnListHandler會返回結果中指定的列)
1 import org.apache.commons.dbutils.QueryRunner; 2 import org.apache.commons.dbutils.handlers.ColumnListHandler; 3 import util.JdbcUtil2; 4 5 import java.sql.Connection; 6 import java.sql.SQLException; 7 import java.util.List; 8 9 public class UtilsSelectColumnListHandler { 10 public static void main(String[] args) throws SQLException { 11 //創建dbUtils里面的QueryRunner對象 12 QueryRunner queryRunner = new QueryRunner(); 13 //sql語句 14 String sql = "select * from stu where age>?"; 15 //獲取數據庫連接 16 Connection connection = JdbcUtil2.getConnection(); 17 //存參數值的數組 18 Object[] params = {16}; 19 //執行查詢,並以數組的形式返回查詢結果(new ColumnListHandler<>()返回結果中指定的列) 20 List<Object> strs = queryRunner.query(connection,sql, new ColumnListHandler<>("name"),params); 21 System.out.println(strs); 22 for(Object item:strs){ 23 System.out.println(item); 24 } 25 //關閉數據庫連接 26 connection.close(); 27 } 28 }
5.10、創建UtilsSelectScalarHandler類,查詢單數據(ScalarHandler返回分組函數的值)
1 import org.apache.commons.dbutils.QueryRunner; 2 import org.apache.commons.dbutils.handlers.ScalarHandler; 3 import util.JdbcUtil2; 4 5 import java.sql.Connection; 6 import java.sql.SQLException; 7 8 public class UtilsSelectScalarHandler { 9 public static void main(String[] args) throws SQLException { 10 //創建dbUtils里面的QueryRunner對象 11 QueryRunner queryRunner = new QueryRunner(); 12 //sql語句 13 String sql = "select max(age) from stu"; 14 //獲取數據庫連接 15 Connection connection = JdbcUtil2.getConnection(); 16 //存參數值的數組 17 Object[] params = {}; 18 //執行查詢,並以數組的形式返回查詢結果(new ScalarHandler<>()返回分組函數的值) 19 int age = queryRunner.query(connection,sql, new ScalarHandler<>(),params); 20 System.out.println(age); 21 //關閉數據庫連接 22 connection.close(); 23 } 24 }
6、用c3p0連接池,連接數據庫
6.1、在scr——pom.xml文件里,引入c3p0包
1 <dependency> 2 <groupId>c3p0</groupId> 3 <artifactId>c3p0</artifactId> 4 <version>0.9.1.2</version> 5 </dependency>
6.2、在src——main——resources下增加c3p0-config.mxl文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <c3p0-config> 4 <default-config> 5 <!-- 配置數據庫驅動 --> 6 <property name="driverClass">com.mysql.jdbc.Driver</property> 7 <!-- 配置數據庫鏈接地址 --> 8 <property name="jdbcUrl">jdbc:mysql://rm-uf9776rxego.mysql.rds.aliyuncs.com:3306/study</property> 9 <!-- 配置數據庫用戶名 --> 10 <property name="user">root</property> 11 <!-- 配置數據庫密碼 --> 12 <property name="password">whmilyY123!</property> 13 14 <!-- 擴展配置 --> 15 <!-- 獲取連接超時設置,默認是一直等待,單位毫秒 --> 16 <property name="checkoutTimeout">30000</property> 17 <!--每多少秒檢查所有連接池中的空閑連接。Default: 0 --> 18 <property name="idleConnectionTestPeriod">30</property> 19 <!-- 初始化連接池的連接數 --> 20 <property name="initialPoolSize">10</property> 21 <!--最大空閑時間,多少秒內未使用則連接被丟棄。若為0則永不丟棄。Default: 0 --> 22 <property name="maxIdleTime">30</property> 23 <!--連接池中保留的最大連接數。Default: 15 --> 24 <property name="maxPoolSize">100</property> 25 <!-- 連接池中保留的最小連接數 --> 26 <property name="minPoolSize">10</property> 27 <!--JDBC的標准參數,用以控制數據源內加載的PreparedStatements數量。但由於預緩存的statements屬於單個connection而不是整個連接池。所以設置這個參數需要考慮到多方面的因素。如果maxStatements與maxStatementsPerConnection均為0,則緩存被關閉。Default:0 --> 28 <property name="maxStatements">200</property> 29 <!--maxStatementsPerConnection定義了連接池內單個連接所擁有的最大緩存statements數。Default: 0 --> 30 <property name="maxStatementsPerConnection">200</property> 31 <!--c3p0是異步操作的,緩慢的JDBC操作通過幫助進程完成。擴展這些操作可以有效的提升性能 通過多線程實現多個操作同時被執行。Default:3 --> 32 <property name="numHelperThreads">3</property> 33 </default-config> 34 </c3p0-config>
6.3、在src——main——java——util里添加DataSourceUtils類
1 package util; 2 3 import com.mchange.v2.c3p0.ComboPooledDataSource; 4 5 import javax.sql.DataSource; 6 import java.sql.Connection; 7 import java.sql.ResultSet; 8 import java.sql.SQLException; 9 import java.sql.Statement; 10 11 public class DataSourceUtils { 12 private static ComboPooledDataSource ds = new ComboPooledDataSource(); 13 14 /** 15 * 獲取數據源 16 * 17 * @return 連接池 18 */ 19 public static DataSource getDataSource() { 20 return ds; 21 } 22 23 /** 24 * 釋放資源 25 * 26 * @param conn 27 * @param st 28 * @param rs 29 */ 30 public static void CloseResource(Connection conn, Statement st, ResultSet rs) { 31 closeResultSet(rs); 32 closeStaement(st); 33 closeConn(conn); 34 } 35 36 /** 37 * 獲取連接 38 * 39 * @return 連接 40 * @throws SQLException 41 */ 42 public static Connection getConnection() throws SQLException { 43 return ds.getConnection(); 44 } 45 46 /** 47 * 釋放連接 48 * 49 * @param conn 連接 50 */ 51 public static void closeConn(Connection conn) { 52 if (conn != null) { 53 try { 54 conn.close(); 55 } catch (SQLException e) { 56 e.printStackTrace(); 57 } finally { 58 conn = null; 59 } 60 } 61 } 62 63 /** 64 * 釋放語句執行者 65 * 66 * @param st 語句執行者 67 */ 68 public static void closeStaement(Statement st) { 69 if (st != null) { 70 try { 71 st.close(); 72 } catch (SQLException e) { 73 e.printStackTrace(); 74 } finally { 75 st = null; 76 } 77 } 78 } 79 80 /** 81 * 釋放結果集 82 * 83 * @param rs 結果集 84 */ 85 public static void closeResultSet(ResultSet rs) { 86 if (rs != null) { 87 try { 88 rs.close(); 89 } catch (SQLException e) { 90 e.printStackTrace(); 91 } finally { 92 rs = null; 93 } 94 } 95 } 96 }
6.4、新建C3p0Select類,用數據庫連接池的方式查詢
1 import org.apache.commons.dbutils.QueryRunner; 2 import org.apache.commons.dbutils.handlers.BeanListHandler; 3 import util.DataSourceUtils; 4 5 import java.sql.SQLException; 6 import java.util.List; 7 8 public class C3p0Select { 9 public static void main(String[] args) throws SQLException { 10 //創建dbUtils里面的QueryRunner對象,並獲取數據庫連接 11 QueryRunner queryRunner = new QueryRunner(DataSourceUtils.getDataSource()); 12 //sql語句 13 String sql = "select * from stu where age>?"; 14 //存參數值的數組 15 Object[] params = {16}; 16 //執行查詢,並以數組的形式返回查詢結果(new BeanListHandler()返回查詢到的所有記錄,並轉成對象) 17 List<Student> students = queryRunner.query(sql, new BeanListHandler<Student>(Student.class),params); 18 System.out.println(students); 19 } 20 }