博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JDBC+XML+DOM4J
阅读量:2386 次
发布时间:2019-05-10

本文共 6736 字,大约阅读时间需要 22 分钟。

利用xml文件封装数据库配置信息

xml文件放在src目录下
/testjdbc1/src/DBUtil.xml 

mysql
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@localhost:1521:orcl
scott
tiger
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/test
root
clc

java文件解析xml文件,封装方法 

/testjdbc1/src/dbutil/JDBCUtil.java  

package dbutil;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.List;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class JDBCUtil {    public static String className;    public static String url ;    public static String user;    public static String pwd ;    /**     * DOM4j解析xml     */    static {        try {            // 开始解析配置文件            SAXReader saxReader = new SAXReader();            // 以流的方式读取配置文件            InputStream inputStream = JDBUtil.class.getClassLoader().getResourceAsStream("DBUtil.xml");            // 开始配置文件            Document document = saxReader.read(inputStream);            // 获取根节点            Element rootElement = document.getRootElement();            // 获取要选择的数据库类型            String databaseType = rootElement.elementText("database-type");            // 判断数据库类型是否为空            if (databaseType != null) {                // 遍历出数据库的配置信息                List
elements = rootElement.elements("database"); for (Element element : elements) { // 判断数据库是否一致 if (databaseType.equals(element.attributeValue("type"))) { // 获取当前元素的所有子元素 className = element.elementText("className"); url = element.elementText("url"); user = element.elementText("user"); pwd = element.elementText("pwd"); } } // 使用静态代码块加载驱动 Class.forName(className); } else { System.out.println("您的配置文件数据库类型【database-type】有误,请重新配置"); } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 创建JDBC连接 connection的连接和事务的提交方式 */ public static Connection createConnection() { return createConnection(true); } // 事务的提交 private static Connection createConnection(boolean autoCommit) { // 声明连接 Connection connection = null; try { // 获取连接 connection = DriverManager.getConnection(url, user, pwd); // 事务的提交方式 connection.setAutoCommit(autoCommit); System.out.println("数据库连接成功"); } catch (SQLException e) { System.out.println("您的数据库详细配置有误url【" + url + "】user【" + user + "】pwd【" + pwd + "】"); e.printStackTrace(); } return connection; } /** * 获取发送器 statement */ public static Statement createStatemen(Connection connection) { Statement statement = null; try { statement = connection.createStatement(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return statement; } /** * 获取预处理发送器 */ public static PreparedStatement createPreparedStatement(Connection connection, CharSequence sql) { PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(sql.toString()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return preparedStatement; } /** * 关闭连接connection */ private static void closeConnection(Connection connection) { if (connection != null) { try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 关闭发送器statement */ private static void closeStatement(Statement statement) { if (statement != null) { try { statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 关闭连接resultSet */ private static void closeResultSet(ResultSet resultSet) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 关闭所有连接 connection statement resultSet */ public static void closeAll(Connection connection, Statement statement, ResultSet resultSet) { closeResultSet(resultSet); closeStatement(statement); closeConnection(connection); }}

 

测试连接

package testoracle;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import dbutil.JDBUtil;public class TestDBUtil {public static void main(String[] args) {    //声明连接    Connection connection=null;    Statement statement=null;    ResultSet resultSet=null;    //sql    String sql="SELECT * FROM EMP";    try {    //获取连接    connection=JDBUtil.createConnection();    //获取发送器    statement=JDBUtil.createStatement(connection);    //发送sql语句        resultSet=statement.executeQuery(sql);        while (resultSet.next()) {            System.out.print(resultSet.getString(1)+"\t");            System.out.print(resultSet.getString(2)+"\t");            System.out.print(resultSet.getString(3)+"\t");            System.out.print(resultSet.getString(4)+"\t");            System.out.println(resultSet.getString(5)+"\t");        }    } catch (SQLException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }finally {        //关闭连接        JDBUtil.closeAll(statement, connection, resultSet);    }    }}

 

数据库中插入日期类型的数据的方式:

preparedStatement.setTimestamp(2, new java.sql.Timestamp(user.getUpdateTime().getTime())); 

 

posted @
2017-05-31 10:53 阅读(
...) 评论(
...)

转载地址:http://mujab.baihongyu.com/

你可能感兴趣的文章
Configuring Server 2008 for RADIUS Authentication
查看>>
Anti-sec安全培训 部分试看视频
查看>>
FreeBSD kernel NFS client local vulnerabilities
查看>>
JXplorer 的简单使用
查看>>
如何启用 LDAP 签名 Windows Server 2008 中
查看>>
获取ngnix,apache,php,mysql的编译参数 zz from xi4oyu
查看>>
使用ettercap嗅探ssh口令
查看>>
POET : Padding Oracle Exploit Tool
查看>>
WordPress brute forcing
查看>>
Simple Log File Analyzer
查看>>
数据同步
查看>>
软件安全开发生命周期读书笔记
查看>>
openldap安装笔记
查看>>
GIT中文教程
查看>>
cobit成功案例
查看>>
Linux下的内网反弹实例
查看>>
Apache 漏洞之后缀名解析漏洞
查看>>
Sun Java Web Server version 7.0 update 7 remote stack overflow exploit
查看>>
Command execution with a MySQL UDF
查看>>
more with rpcclient
查看>>