标签:ack 新建 spring sql数据库 style artifact 执行 连接数据库 col
注:测试使用maven构建的项目,如需学习maven构建项目,请先到maven分类学习如何构建maven项目
1、创建一个maven项目,然后找到pom.xml文件,打开(第一次打开不是通过.xml文件格式打开,可通过右键 -- Open With -- XML Editor 打开)
打开后在配置文件中添加依赖,也就是jar包。
2、添加依赖:
(1)c3p0核心依赖
<dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.1</version> </dependency>
(2)MySQL依赖
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency>
(3)JDBC工具类库
<dependency> <groupId>commons-dbutils</groupId> <artifactId>commons-dbutils</artifactId> <version>1.2</version> </dependency>
3、新建一个数据库,执行数据库脚本代码,创建表
drop table if exists customer; create table customer ( cus_id int not null auto_increment, cus_name varchar(10), cus_age int, primary key (cus_id) );
4、新建一个实体类,对应数据库中的表结构
1 public class Customer { 2 private String cus_id; 3 private String cus_name; 4 private int age; 5 public Customer(String cus_id, String cus_name, int age) { 6 super(); 7 this.cus_id = cus_id; 8 this.cus_name = cus_name; 9 this.age = age; 10 } 11 public Customer() { 12 super(); 13 } 14 public String getCus_id() { 15 return cus_id; 16 } 17 public void setCus_id(String cus_id) { 18 this.cus_id = cus_id; 19 } 20 public String getCus_name() { 21 return cus_name; 22 } 23 public void setCus_name(String cus_name) { 24 this.cus_name = cus_name; 25 } 26 public int getAge() { 27 return age; 28 } 29 public void setAge(int age) { 30 this.age = age; 31 } 32 @Override 33 public String toString() { 34 return "Customer [cus_id=" + cus_id + ", cus_name=" + cus_name + ", age=" + age + "]"; 35 } 36 37 }
5、新建一个工具类,用于连接设置数据源和连接数据库信息(连接数据库的地址和用户根据自己的来修改)
我的url:jdbc:mysql://192.168.1.222:3306/test ;用户名:root 密码:root
1 import java.beans.PropertyVetoException; 2 import java.sql.Connection; 3 import java.sql.PreparedStatement; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import com.mchange.v2.c3p0.ComboPooledDataSource; 7 8 public class JDBCUtils { 9 //private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); 10 static ComboPooledDataSource dataSource = new ComboPooledDataSource(); 11 //静态代码块自动装载,连接数据库的参数 12 static { 13 14 try { 15 dataSource.setDriverClass("com.mysql.jdbc.Driver"); 16 dataSource.setJdbcUrl("jdbc:mysql://192.168.1.222:3306/test"); 17 dataSource.setUser("root"); 18 dataSource.setPassword("root"); 19 dataSource.setMaxPoolSize(10);//最大连接数 20 dataSource.setMinPoolSize(0);//最小连接数 21 dataSource.setInitialPoolSize(5);//初始化连接数 22 dataSource.setAcquireIncrement(5);//连接增量 23 } catch (PropertyVetoException e) { 24 e.printStackTrace(); 25 } 26 27 } 28 //获得连接 29 public static Connection getCon() { 30 try { 31 return dataSource.getConnection(); 32 } catch (Exception e) { 33 e.printStackTrace(); 34 return null; 35 } 36 } 37 public static ComboPooledDataSource getDataSource() { 38 return dataSource; 39 } 40 //关闭连接 41 public static void closeCon(Connection conn,PreparedStatement pst,ResultSet rs) { 42 if(rs != null) { 43 try { 44 rs.close(); 45 } catch (SQLException e) { 46 e.printStackTrace(); 47 } 48 finally {rs = null;} 49 } 50 if(pst != null) { 51 try { 52 pst.close(); 53 } catch (SQLException e) { 54 e.printStackTrace(); 55 } 56 finally {pst = null;} 57 } 58 if(conn != null) { 59 try { 60 conn.close(); 61 } catch (SQLException e) { 62 e.printStackTrace(); 63 } 64 finally {conn = null;} 65 } 66 } 67 public void closeCon(Connection conn) { 68 if(conn != null) { 69 try { 70 conn.close(); 71 } catch (SQLException e) { 72 e.printStackTrace(); 73 } 74 finally {conn = null;} 75 } 76 } 77 }
6、建立一个测试类,测试数据连接
1 import java.sql.Connection; 2 import java.sql.SQLException; 3 import org.apache.commons.dbutils.QueryRunner; 4 import org.apache.commons.dbutils.handlers.BeanHandler; 5 import org.junit.Test; 6 import com.li.spring.pojo.Customer; 7 import com.li.spring.utils.JDBCUtils; 8 9 public class ConnTest { 10 //要使用QueryRunner,需要导入commons.dbutils的jar包 11 //通过工具类获取数据源 12 QueryRunner queryrunner = new QueryRunner(JDBCUtils.getDataSource()); 13 //获取连接 14 Connection conn = JDBCUtils.getCon(); 15 @Test 16 public void test() throws SQLException { 17 //新建一个对象,接收查询的结果集 18 Customer cus = new Customer(); 19 //查询语句 20 String sql = "select * from customer"; 21 cus = (Customer) queryrunner.query(conn, sql,new BeanHandler(Customer.class)); 22 //System.out.println(conn.getClass().getName()); 23 System.out.println(cus.toString()); 24 //关闭资源 25 new JDBCUtils().closeCon(conn); 26 } 27 28 }
7、运行测试类
标签:ack 新建 spring sql数据库 style artifact 执行 连接数据库 col
原文地址:http://www.cnblogs.com/lisheng-cn/p/7510743.html