1- 工具类 JDBCUtils
package com.th.JDBCUtils; import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; /** * Created by Administrator on 2018/6/20. */ public class JDBCUtils { private static Connection connection ; private static String className ; private static String url ; private static String user ; private static String password ; static { try { readConfig(); Class.forName(className); connection = DriverManager.getConnection(url, user, password); } catch (Exception e) { e.printStackTrace(); } } private static void readConfig() throws IOException { /* // 原始方法: 这个时候jdbc.properties 配置文件必须放放在工程目录下 // 但是 在给别人工程的时候不可能把整个工程都给别人,只能给bin目录下的东西。 // 所有 配置文件需要放在src 目录下,(bin 目录下会自动生成一个一样的配置文件) Properties properties = new Properties(); properties.load(new FileInputStream("jdbc.properties")); className = properties.getProperty("className"); url = properties.getProperty("url"); user = properties.getProperty("user"); password = properties.getProperty("password"); */ //使用类的加载器 只需要知道文件名字就会去bin 目录下去查找这个文件 InputStream inputStream = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); properties.load(inputStream); className = properties.getProperty("className"); url = properties.getProperty("url"); user = properties.getProperty("user"); password = properties.getProperty("password"); } public static Connection getConnection(){ return connection; } public static void close(Connection conn, Statement statement,ResultSet resultSet){ if ( resultSet != null ) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if ( statement !=null ) { try { statement.close(); } catch (SQLException e){ e.printStackTrace(); } } if ( conn !=null ) { try { conn.close(); } catch (SQLException e){ e.printStackTrace(); } } } }
2- 配置文件:
className=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mydatabase user=root password=root注意: 配置文件如果方放在工程的根目录下在bin 目录下就不会自动生成一样的配置文件,如果配置文件放在src 目录下,在bin 目录下就可以自动生成同样的配置文件。建议放在src 目录下。
3- 测试类:
package com.th.JDBC; import com.th.JDBCUtils.JDBCUtils; import com.th.domain.Sort; import java.sql.*; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2018/6/20. */ public class Demo2_jdbc { public static void main(String[] args) throws ClassNotFoundException, SQLException { /*//1- 加载驱动 Class.forName("com.mysql.jdbc.Driver"); //2- 获取连接数据库对象 String url = "jdbc:mysql://localhost:3306/mydatabase"; String user = "root"; String password = "root"; Connection connection = DriverManager.getConnection(url, user, password); */ Connection connection = JDBCUtils.getConnection(); //3- 获取预处理对象 String sql = "select * from sort"; PreparedStatement preparedStatement = connection.prepareStatement(sql); //4- 执行sql 语句并获取结果 ResultSet resultSet = preparedStatement.executeQuery(); //5- 处理结果 List<Sort> list = new ArrayList<Sort>(); while ( resultSet.next() ){ Sort sort = new Sort(resultSet.getInt("sid"), resultSet.getString("sname"), resultSet.getDouble("sprice"), resultSet.getString("sdesc")); list.add(sort); } System.out.println(list); //6- 关闭资源 /* preparedStatement.close(); connection.close(); */ JDBCUtils.close(connection,preparedStatement,resultSet); } }
4- 总结:
a- 读取配置文件就两种方法
1- 一般的new FileInputStream(“文件名称”);// 文件需要放在工程根目录下;
2- 使用类加载器
类名.class.getClassLoader().getResourceAsStream("文件名称");//配置文件需要放在src目录下;
InputStream inputStream = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");