数据库配置
1. ORM
Object Relationship Mapping 对象关系映射
类和表结构对应
属性和字段对应
对象和记录对应
2. JDBC
java database connnectivity
官方定义的一套操作所有关系型数据库的规则。
各家厂商实现这套接口,提供数据库驱动jar包,使用接口编程,真正执行的是jar包
2.1. 初始化
1 2 3 4
| driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/login?useSSL=false user=root password=mysql123//
|
1 2 3 4 5 6 7 8
| String driver,url,user,password;
Properties prop = new Properties(); prop.load(new FileInputStream("src/db.properties")); driver=prop.getProperty("driver"); url=prop.getProperty("url"); user=prop.getProperty("user"); password=prop.getProperty("password");
|
2.2. 注册驱动
1 2 3 4 5 6 7 8
| Class.forName(driver);
try{ java.sql.DriverManager.registerDriver(new Driver()); }catch(SQLException e){ throw new RuntimeException("Can't register driver!"); }
|
2.3. 建立连接
1
| Connection conn=DriverManager.getConnection(url,user,password);
|
2.4. 构建并执行语句
boolean execute(String sql)
int executeUpdate(String sql)
:DML(insert,update,delete),DDL(create alter, drop)
ResultSet executeQuery(String sql)
:DQL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| Statement ment = conn.createStatement(); ment.exectue("sql语句");
PreparedStatement pre = conn.prepareStatement("insert into test(name) values (?)"); pre.setString(1,'a'); int resultset = pre.executeUpdate(); ResultSet result = pre.executeQuery();
CallableStatement callment=conn.perpareCall("{call add_pro(?,?,?)}"); callment.setInt(1,4); callment.setInt(2,1); callment.registerOutParameter(3,Types.INTEGER); ResultSet set = call.executeQuery();
|
2.5. 返回结果
1 2 3 4
| ResultSet rs = ment.executeQuery(sql); while(rs.next()){ rs.getString(1); }
|
2.6. 关闭连接
1 2 3
| rs.close(); ment.close(); conn.close();
|
2.7. 存储图片
1 2 3 4
| File file = new File(path); PrepareStatement ment = conn.prepareStatement("insert into test(img) values(?)");
ment.setBinaryStream(1,new FileInputStream(file),(int)file.length());
|
2.8. 取图片
1 2
| Blob blob = rs.getBlob(1); ImageIcon icon = new ImageIcon(blob.getBytes(1L,(int)blob.length()));
|
表结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| ResultSet rs = ment.executeQuery(); ResultSetMetaData data= rs.getMetaData();
int column = data.getColumnCount(); String name = data.getColumnName(1); Object obj= data.getColumnType(1);
DatabaseMetaData data= conn.getMetaData(); ResultSet rs = data.getTableTypes();
rs = data.getTables(null,null,"%",new String[]{"TABLE"});
rs = data.getPrimaryKeys(null,null,"test");
|
3. 事务处理
包含多个步骤的业务操作,被事务管理
1 2 3 4 5 6 7 8
| start transaction;
rollback;
commit;
set @@autocommit=0;
|
3.1. 四大特征
- 原子性 :最小逻辑执行体
- 一致性: 从一个一致性状态到另一个一致性状态
- 隔离性: 各个事务执行互不干扰
- 持续性:改变永久存储
1 2 3 4 5 6 7 8 9
| conn.setAutoCommit(false); try{
}catch(Expection e){ conn.rollback(); } conn.commit();
|
3.2. 隔离级别
脏读:一个事务,读取到另一个事务没有提交的数据
不可重复读:同一个事务中,两次读取到的数据不一样
幻读:一个事务操作数据表中所有记录,另一个事务添加一个数据,第一个事务查询不到自己的修改
1 2 3
| select @@tx_isolation;
set global transaction level read committed;
|
3.3. 批量处理
1 2 3 4 5 6 7 8
| Statement st = conn.createStatement(); conn.setAutoCommit(false);
st.addBatch(sql);
st.executeBatch(); conn.commit();
|
4. 连接池
4.1. C3P0数据源
c3p0-config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/login</property> <property name="user">root</property> <property name="password">mysql123//</property> <property name="initialPoolSize">5//</property> <property name="maxPoolSize">10</property> <property name="checkoutTimeout">1000</property> </default-config> <named-config name="指定名称配置"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/login</property> <property name="user">root</property> <property name="password">mysql123//</property> <property name="initialPoolSize">5//</property> <property name="maxPoolSize">8</property> <property name="checkoutTimeout">1000</property> </named-config> </c3p0-config>
|
配置数据源
1 2 3 4 5 6
| import javax.sql.DataSource;
DataSource source = new ComboPooledDataSource();
DataSource source = new ComboPooledDataSource("指定名称配置");
|
4.2. Druid数据源
阿里巴巴
druid.properties
1 2 3 4 5 6 7 8
| driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/login username=root password=mysql123//
initialSize = 5 maxActive=10 maxWait=3000
|
配置
1 2 3 4 5
| Properties prop = new Properties(); prop.load(new FileInputStream("src/druid.properties"));
DataSource source = DruidDataSourceFactory.createDataSource(prop);
|