利用xml文件封装数据库配置信息
xml文件放在src目录下
/testjdbc1/src/DBUtil.xml
<?xml version="1.0" encoding="GBK" ?> <sxtConfig> <!-- 本次 连接 mysql 数据库--> <database-type>mysql</database-type> <database type="oracle"> <className>oracle.jdbc.driver.OracleDriver</className> <url>jdbc:oracle:thin:@localhost:1521:orcl</url> <user>scott</user> <pwd>tiger</pwd> </database> <database type="mysql"> <className>com.mysql.jdbc.Driver</className> <url>jdbc:mysql://localhost:3306/test</url> <user>root</user> <pwd>clc</pwd> </database> </sxtConfig>
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<Element> 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()));
时间: 2024-10-06 05:36:34