【石家庄校区】WEB总结
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class Utils {
private static final ComboPooledDataSource ds = new ComboPooledDataSource();
public static Connection getConnection(){
try {
return ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void close(ResultSet rs,PreparedStatement pst,Connection con){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(pst != null){
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
pst = null;
}
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
con = null;
}
}
public static void close(PreparedStatement pst,Connection con){
close(null,pst,con);
}
}
配置文件
E:/YouDao/BiJi/qq231AE87F4D7B50A0A81C5C35BDCF40A1/6eb97b0ba2644a4ea5f2000b700fb5a0/cd339b8344b349b0948d356a3a747904.jpg
自定义连接池:MyDataSource
public class MyDataSouce implements DataSource{
private List< Connection> list = new ArrayList<>();
{
try {
Class.forName("com.mysql.jdbc.Driver");
addConn();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void addConn() {
try {
for (int i = 0; i < 3; i++) {
list.add(DriverManager.getConnection("jdbc:mysql:///day09", "root", "123"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public Connection getConnection() throws SQLException {
if(list.size()==0){
addConn();
}
Connection conn = list.remove(0);
//增强conn的功能
BigConnetion bigConnetion = new BigConnetion(conn, list);
return bigConnetion;
}
适配器:ConnectionAdapter
public class ConnectionAdapter implements Connection{
protected Connection conn;
public ConnectionAdapter(Connection conn){
this.conn =conn;
}
增强:BigConnection
public class BigConnetion extends ConnectionAdapter{
private List<Connection> list;
public BigConnetion(Connection conn,List<Connection> list){
super(conn);
this.list = list;
}
@Override
public void close() throws SQLException {
//放回到池子
list.add(conn);
}
}
测试类
package test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test {
@org.junit.Test
public void test() throws SQLException {
MyDataSouce dataSouce = new MyDataSouce();
Connection connection = dataSouce.getConnection();
PreparedStatement pst = connection.prepareStatement("select * from student");
ResultSet rs = pst.executeQuery();
while (rs.next()) {
System.out.println(rs.getObject("sid"));
}
rs.close();
pst.close();
connection.close();
}
}
Assert.assertEquals(3,2)
前边是期望值,后边是实际值
实际值和期望值不一样时,单元测试棕色
Arrays.asList:数组转成集合
为什么装饰者设计模式要实现相同的接口?
要保证被装饰对象的全部功能
一般和适配器设计模式一起使用。
为了解决超多的方法重写而存在的
【JDBC的元数据MataData】(了解)-- 编写通用性较高的代码.
DatabaseMetaData:获得数据库连接的信息,获得数据库的表的信息.
* 获得数据库元数据:Connection中getMetaData();
ParameterMetaData:获得SQL中的参数的个数及类型.
* 获得参数元数据:PreparedStatement中getParameterMetaData()
ResultSetMetaData:获得结果集中的列名及列的类型.
* 获得结果集元数据:ResultSet中getMetaData ()
【元数据的使用】
@Test
/**
* 数据库元数据
*/
public void demo1(){
Connection conn = null;
conn = JDBCUtils2.getConnection();
// 获得数据库元数据:
try {
DatabaseMetaData metaData = conn.getMetaData();
System.out.println("获得驱动名称:"+metaData.getDriverName());
System.out.println("获得驱动URL:"+metaData.getURL());
System.out.println("获得用户名:"+metaData.getUserName());
// 获得表中的主键:
ResultSet rs = metaData.getPrimaryKeys(null, null, "category");
if(rs.next()){
String name = rs.getString("COLUMN_NAME");
System.out.println(name);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test
/**
* 参数元数据:
*/
public void demo2(){
Connection conn = null;
PreparedStatement stmt = null;
try{
conn = JDBCUtils2.getConnection();
String sql = "update category set cname = ? where cid = ?";
stmt = conn.prepareStatement(sql);
ParameterMetaData metaData = stmt.getParameterMetaData();
int count = metaData.getParameterCount();
System.out.println(count);
}catch(Exception e){
}
}
@Test
/**
* 结果集元数据:
*/
public void demo3(){
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = JDBCUtils2.getConnection();
String sql = "select * from category";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
ResultSetMetaData metaData = rs.getMetaData();
int count = metaData.getColumnCount();
for(int i = 1;i<=count ;i++){
String name = metaData.getColumnName(i);
String type = metaData.getColumnTypeName(i);
System.out.println(name+type);
}
}catch(Exception e){
}
}
DOM4J的入门案例步骤:
* 【步骤一】导入jar包.dom4j-1.6.1.jar
* 【步骤二】创建解析器 SaxReader
* 【步骤三】解析文档获得代表文档的Document对象. —— SAXReader 的 read()方法获取Document。
* 【步骤四】获得跟节点.——通过Document对象的getRootElement()得到根节点
* 【步骤五】从跟节点下查找其他的节点. 使用根节点获取他下面的节点element() elements(“标签名称”)
* 代码实现:
@Test
/**
* 获得元素的内容:查询的操作.
*/
public void demo1() throws Exception{
// 创建解析器
SAXReader reader = new SAXReader();
// 解析XML的文档
Document document = reader.read("xml/demo1.xml");
// 获得跟节点
Element root = document.getRootElement();
System.out.println(root.getName());
// 查找跟节点下的子节点. element() elements();
Element pElement = root.element("person"); // 查找的是第一个person元素
// root.elements("person").get(1); // 查找的是第二个person元素
Element nElement = pElement.element("name");
Element aElement = pElement.element("age");
Element sElement = pElement.element("sex");
System.out.println(nElement.getText());
System.out.println(aElement.getText());
System.out.println(sElement.getText());
}
XPath:
* dom4j支持XPath的jar包.
* jaxen-1.1-beta-6.jar
* dom4j的XPath支持的API:
* List document.selectNodes(String xPath);
* Node document.selectSingleNode(String xPath);
* 代码:
@Test
/**
* DOM4J的XPath的写法:
*/
public void demo2() throws Exception{
// 创建解析器:
SAXReader reader = new SAXReader();
// 解析XML返回Document对象.
Document document = reader.read("xml/demo1.xml");
/*List<Node> list = document.selectNodes("//name");
for (Node node : list) {
Element element = (Element) node;
System.out.println(element.getText());
}*/
List<Node> list = document.selectNodes("//person['@id']");
for (Node node : list) {
Element element = (Element) node;
System.out.println(element.attributeValue("id"));
}
}
|