最近这段时间一直在用SSM框架做增删改查,突然想把以前还不会用框架的时候,综合百度和各种资料结合API文档抄袭而来的数据层的封装分享给大家。这边先封装一个DButil。
我这个封装就是烂大街的那种,没什么特别。
//DButil.java package com.yck.database; import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; public class DButil { private static String username; private static String password; private static String url; private static String driver; static { getDatabaseInfo("jdbc.properties"); try { Class.forName(driver); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void getDatabaseInfo(String path) { InputStream in = DButil.class.getClassLoader().getResourceAsStream(path); Properties p = new Properties(); try { p.load(in); username = p.getProperty("jdbc.username"); password = p.getProperty("jdbc.password"); url = p.getProperty("jdbc.url"); driver = p.getProperty("jdbc.driver"); } catch (IOException e) { e.printStackTrace(); } } public static Connection getConnection() { Connection conn = null; try { conn = DriverManager.getConnection(url,username,password); }catch (SQLException e) { e.printStackTrace(); } return conn; } public static void closeConnection(Connection connection) { try { if(connection != null) { connection.close(); connection = null; } } catch (SQLException e) { e.printStackTrace(); } } public static PreparedStatement prepareStatement(Connection connection,String sql) { PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(sql); } catch (SQLException e) { e.printStackTrace(); } return preparedStatement; } public static void closePreparedStatement(PreparedStatement preparedStatement) { try { if(preparedStatement != null) { preparedStatement.close(); preparedStatement = null; } } catch (SQLException e) { e.printStackTrace(); } } public static Statement createStatement(Connection connection) { Statement statement = null; try { statement = connection.createStatement(); } catch (SQLException e) { e.printStackTrace(); } return statement; } public static void closeStatement(Statement statement) { try { if(statement != null) { statement.close(); statement = null; } } catch (SQLException e) { e.printStackTrace(); } } public static ResultSet getResultSet(Statement statement,String sql) { ResultSet resultSet = null; try { resultSet = statement.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return resultSet; } public static void closeResultSet(ResultSet resultSet) { try { if(resultSet != null) { resultSet.close(); resultSet = null; } } catch (SQLException e) { e.printStackTrace(); } } }
这里是数据库连接需要的jdbc.properties,我们是通过DButil类的读取properties文件的办法来将DButil实例化的。
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=true jdbc.username=root jdbc.password=8888888
数据库写了个很简单的来测试
CREATE DATABASE test; USE test; CREATE TABLE t_user ( id INT AUTO_INCREMENT NOT NULL, name VARCHAR(20), age INT, PRIMARY KEY(id) )ENGINE=InnoDB DEFAULT CHARSET=utf8
手动插了两条数据,数据库的内容如下
下面我们来测试一下,新建一个Test类
package com.yck.database; import java.sql.*; public class Test { public static void main(String[] args) { Connection conn = DButil.getConnection(); Statement stam =DButil.createStatement(conn); String sql = "select * from t_user"; ResultSet rs = null; try { rs = stam.executeQuery(sql); while(rs.next()) { System.out.println(rs.getString("name")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } DButil.closeResultSet(rs); DButil.closeStatement(stam); DButil.closeResultSet(rs); } }
运行结果如下,说明我们封装的数据库是能够连接上的
时间: 2024-10-26 00:31:39