DBCP ---- "Database" and "connection pool" connection establishment process, dbcp ----
1 1 public class DBCPTest {2 2/* 3 3 * use the BasicDataSource class and establish a connection with the database through url, 4 and diverClass, username, password, and 5, return a DataSource connection pool object 6 6 */7 7 public DataSource getDS () {8 8 BasicDataSource ds = new BasicDataSource (); 9 9 ds. setDriverClassName ("com. mysql. jdbc. driver "); 10 10 ds. setUsername ("root"); 11 11 ds. setPassword ("4230"); 12 12 ds. setUrl ("jdbc: mysql: // localhost: 3306/jdbcdemo"); 13 13 return ds; 14 14 15 15} 16 16 17 17/* 18 18 * test query, use the 19 19 getConnection () method of the returned DataSource Connection pool object to create a Connection object. The subsequent steps omit 20 */21 21 @ Test22 22 public void testDBCPTest () throws Exception {23 23 24 24 Connection connec = getDS (). getConnection (); 25 25 String SQL = "SELECT * FROM student"; 26 26 PreparedStatement ps = connec. prepareStatement (SQL); 27 27 ResultSet rSet = ps.exe cuteQuery (); 28 28 while (rSet. next () {29 29 System. out. println (rSet. getLong (1) + "-" + rSet. getString (2) + "-" + rSet. getInt (3); 30 30} 31 31 JdbcUtil. close (rSet, ps, connec); 32 32} 33 33}