设为首页 加入收藏

TOP

数据库之JDBC操作及实例讲解(三)
2018-03-24 09:50:28 】 浏览:361
Tags:数据库 JDBC 操作 实例 讲解
es = new Properties(); FileInputStream fileInputStream; try { fileInputStream = new FileInputStream("src/dbinfo.properties"); properties.load(fileInputStream); // 读文件 driverClass = properties.getProperty("driverClass"); url = properties.getProperty("url"); user = properties.getProperty("user"); password = properties.getProperty("password"); } catch (Exception e) { e.printStackTrace(); } // 第二种读取配置文件的方法:使用系统类来读取配置文件 ResourceBundle resourceBundle = ResourceBundle.getBundle("dbinfo"); // 获取文件中的数据 driverClass = resourceBundle.getString("driverClass"); url = resourceBundle.getString("url"); user = resourceBundle.getString("user"); password = resourceBundle.getString("password"); // 让驱动类只加载一次 try { Class.forName(driverClass); } catch (ClassNotFoundException e) { e.printStackTrace(); } } // 获取数据库连接的方法 public static Connection getConnection() throws SQLException, ClassNotFoundException { return DriverManager.getConnection(url, user, password); } // 关闭数据库的方法 如果没有结果集需要关闭,直接传空就行 public static void closeAll(ResultSet resultSet, Statement statement, Connection connection) { // 关闭资源前,要做非空判断,防止空指针异常 if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { throw new RuntimeException("资源关闭失败"); } // 加快系统回收的速度 resultSet = null; } if (statement != null) { try { statement.close(); } catch (SQLException e) { throw new RuntimeException("资源关闭失败"); } // 加快系统回收的速度 statement = null; } if (connection != null) { try { connection.close(); } catch (SQLException e) { throw new RuntimeException("资源关闭失败"); } // 加快系统回收的速度 connection = null; } } }
测试 JDBC 工具类

public class TestJDBCUtil {
    @Test
    public void testSelect() {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            // 获取连接
            connection = JDBCUtil.getConnection();
            statement = connection.createStatement();
            String sql = "select * from users";
            resultSet = statement.executeQuery(sql);
            // 处理结果集(把数据的记录封装到对象中)
            ArrayList
  
    arrayList = new ArrayList<>();
            while(resultSet.next()) {
                // 创建 User 对象
                User user = new User();
                user.setId(resultSet.getInt("id"));
                user.setName(resultSet.getString("name"));
                user.setPassword(resultSet.getString("password"));
                user.setEmail(resultSet.getString("email"));
                user.setBirthday(resultSet.getDate("birthday"));
                // 放入集合中
                arrayList.add(user);
            }
            // 遍历查看
            for (User user : arrayList) {
                System.out.println(user);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeAll(resultSet, statement, connection);
        }
    }
}
  

SQL 语句注入问题

public class Login {
    public static void main(String[] args) {
        // 接收用户输入的账号和密码
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入账号:");
        String name = scanner.nextLine();
        System.out.println("请输入密码:");
        String password = scanner.nextLine();
        // 调用查询方法
        DoLogin doLogin = new DoLogin();
        User user = doLogin.findUser(n
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇java之数据库连接JDBC教程 下一篇MySQL数据库管理、表管理及增删改..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目