首页 / XML / JDBC/XML的一些基本使用
JDBC/XML的一些基本使用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JDBC/XML的一些基本使用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5242字,纯文字阅读大概需要8分钟。
内容图文
![JDBC/XML的一些基本使用](/upload/InfoBanner/zyjiaocheng/1154/a222fffa72304870a050049a95b5a4ed.jpg)
一、知识点题目:JDBC核心API的使用
关键字:JDBC核心API
内容:
1)加载<SPAN style=‘font-family: "Times New Roman";‘>JDBC
驱动 "Times New Roman";‘>:Oracle:Class.forName(“oracle.jdbc.driver.OracleDriver”);
MYSQL:Class.forName(“com.mysql.jdbc.Driver”);
2)通过<SPAN style=‘font-family: "Times New Roman";‘>DriverManage获取连接<SPAN style=‘font-family: "Times New Roman";‘>:
Connection conn=
DriverManage.getConnection(String url,String username,String pwd);
其中<SPAN style=‘font-family: "Times New Roman";‘>url:
Oracle=jdbc:oracle:thin:@hostIP:port:databaseName
MYSQL=jdbc:mysql://hostIP:port/databaseName
3)通过<SPAN style=‘font-family: "Times New Roman";‘>Connection创建<SPAN style=‘font-family: "Times New Roman";‘>Statement对象
非预编译<SPAN style=‘font-family: "Times New Roman";‘>:
Statement stat=conn.createStatement();
预编译<SPAN style=‘font-family: "Times New Roman";‘>:
String sql=”select * from table where id=?”;
PreparedStatement stat=conn.PreparedStatement(sql);
4)执行<SPAN style=‘font-family: "Times New Roman";‘>SQL语句<SPAN style=‘font-family: "Times New Roman";‘>,获得结果
stat.setXxx(1,value);
stat.executeUpdate();
stat.execute();
ResultSet rs=stat.executeQurey();
5)获取结果集中的值
rs.next():判断集合里下一个是否有值
rs.getXxx(字段名<SPAN style=‘font-family: "Times New Roman";‘>)或<SPAN style=‘font-family: "Times New Roman";‘>rs.getInt(1)结果集每项第一列的值
6)关闭打开的对象
if(rs!=null){
rs.colse();
}
if(stat!=null){
stat.colse();
}
if(conn!=null){
conn.colse();
}
二、知识点题目:DBUtil工具类的编写
关键字:DBUtil
内容:以mysql实现为例:
方法:private static Connection getMConn()//用于获取连接对象的方法
1 先编写mysql.properties配置文件<SPAN style=‘font-family: "Times New Roman";‘>,用于配置连接所需的字段信息
内容以键值对的形式<SPAN style=‘font-family: "Times New Roman";‘>(key=value):
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/futao
jdbc.username=futao
jdbc.password=1234
2 创建对象来加载配置文件
Properties prop=new Properties();
prop.load(DBUtil.class.getClassLoader().getResourceAsStream("util/mysql.properties"));
3 根据Properties对象的getProperty(key)方法来获取相应的字段名
4 按照知识点一的方法获取连接对象并返回该对象
方法:public static Connection getMConnection()//以事务的方法返回连接对象<SPAN style=‘font-family: "Times New Roman";‘>,并且该对象始终只被一个线程所使用
1.ThreadLocal<Connection> connections=new ThreadLocal<Connection>();
用于存放当前线程所使用的连接对象
2. conn=getMConn();
connections.set(conn);
获取连接并把连接放入ThreadLocal用于当前线程使用
3.返回连接对象
方法:public static void closeConnection()//用户关闭当前线程所使用的连接对象
1.关闭<SPAN style=‘font-family: "Times New Roman";‘>conn连接
2.将ThreadLocal集合里设为null
方法:public static void beginTransaction()//用于启动事务
1.获取当前线程的连接
2.conn.setAutoCommit(false);关闭事务的自动提交
方法:public static void commitTransaction()//用于事务的提交
1.获取当前线程的连接
2.conn.commit();提交事务
3.关闭连接
方法:public static void rollbackTransaction()//用于回滚事务
1.获取当前线程连接
2.conn.rollback();回滚事务
3.关闭连接
三、知识点题目:用于返回类的实例的工厂方法
关键字:Factory 工厂方法
内容:
1.编写配置文件<SPAN style=‘font-family: "Times New Roman";‘>,将需要返回的对象的类与字段对应起来<SPAN style=‘font-family: "Times New Roman";‘>,以键值对的形式<SPAN style=‘font-family: "Times New Roman";‘>(key=value)
例<SPAN style=‘font-family: "Times New Roman";‘>:factory.preperties文件中写入
IAccountDAO=implement.AccountDAOImpl
IStockDAO=implement.StockDAOImpl
2.通过Properties对象加载配置文件
3.方法<SPAN style=‘font-family: "Times New Roman";‘>:public static Object getInstance(String type)//根据type指定的字段在配置文件中匹配并返回相应类的对象
1)获取<SPAN style=‘font-family: "Times New Roman";‘>type对应的<SPAN style=‘font-family: "Times New Roman";‘>value值
2)通过反射Class c=Class.forName(value);获取指定类的类对象
4.通过类对象的newInstance()方法创建一个该类的实例
5.将该实例返回
四、知识点题目:编写DAO的思路
关键字:DAO
内容:
1)根据业务编写实体类entity与数据库中的表一一对应
2)编写接口,定义一些操作数据库的方法
3)编写操作数据库的类实现接口,访问数据库,对数据库进行相关的操作
4)编写业务类,通过调用一系列操作数据的类的方法来实现具体的业务
小结:以上具体表现了在开发中将数据层、业务层等进行分离的开发方法.提高代码的可维护性,减少耦合,是MVC模式的基础
五、知识点题目:DOM4J核心API的使用
关键字:DOM4J XML
内容:
1)向<SPAN style=‘font-family: "Times New Roman";‘>XML中写内容<SPAN style=‘font-family: "Times New Roman";‘>:以<SPAN style=‘font-family: "Times New Roman";‘>Point类为例
1.在内存中创造一棵树
Document doc=DocumentHelper.createDocument();
2.为树添加一个根节点
Element root=doc.addElement("point");
3.在根节点下<SPAN style=‘font-family: "Times New Roman";‘>,为树添加子节点
Element xElement=root.addElement("x");
Element yElement=root.addElement("y");
4.为子节点赋值
xElement.setText(point.getX());
yElement.setText(point.getY());
5.创建字节文件输出流
OutputStream ops=new FileOutputStream("d:/fu/point.xml");
6.可以设置<SPAN style=‘font-family: "Times New Roman";‘>XML写内容时的对齐排版格式
OutputFormat format=OutputFormat.createPrettyPrint();
为带换行的标准格式
7.将内存中树的数据输出到流指定的文件
XMLWriter writer=new XMLWriter(ops);
writer.write(doc);
8.关闭输出流
注<SPAN style=‘font-family: "Times New Roman";‘>:除了在根节点下添加叶子节点外还可以<SPAN style=‘font-family: "Times New Roman";‘>:
A.添加属性root.addAttribute("type", cus.getType());
B.添加<SPAN style=‘font-family: "Times New Roman";‘>CDATA:root.addCDATA(xxx);
等内容.
2)向<SPAN style=‘font-family: "Times New Roman";‘>XML文件中读内容
1.创建字节文件输入流
InputStream ips=new FileInputStream("d:/fu/point.xml");
2.创建读取<SPAN style=‘font-family: "Times New Roman";‘>XML的类
SAXReader reader=new SAXReader();
Document doc=reader.read(ips);
3.获取根节点
Element root=doc.getRootElement();
4.获取属性的值
String type=root.attributeValue("type");
5.获取子节点的值
String x=root.elementText("x");
String y=root.elementText(“y");
6.关闭输入流
原文:http://www.cnblogs.com/DinoFT/p/3567712.html
内容总结
以上是互联网集市为您收集整理的JDBC/XML的一些基本使用全部内容,希望文章能够帮你解决JDBC/XML的一些基本使用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。