
复制public class jdbcConnection { private staticConnectionconnection = null; private static PreparedStatement preparedStatement = null; private static ResultSet resultSet = null; publicstatic void main(String[] args) { try { // 加载数据库驱动 Class.forName("com.mysql.jdbc.Driver"); // 通过驱动管理类获取数据库连接 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/huodd",长文 "root", "1234"); // 定义sql语句 ? 表示占位符 String sql = "select id,username from user where id = ?"; // 获取预处理对象 statement PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement(sql); // 设置参数 第一个参数为 sql 语句中参数的序号(从1开始) 第二个参数为 设置的源码库参数值 preparedStatement.setInt(1, 1); // 向数据库发出sql执行查询 查询出结果集 resultSet = preparedStatement.executeQuery(); // 遍历查询结果集 while (resultSet.next()) { int id = resultSet.getInt("id"); String username = resultSet.getString("username"); // 封装对象 Useruser = new User(); user.setId(id); user.setUsername(username); System.out.println(user); } } catch (Exception ex) { ex.printStackTrace(); } finally { try { // 释放资源 if (resultSet != null) { resultSet.close(); } if (preparedStatement != null) { preparedStatement.close(); } if (connection != null) { connection.close(); } } catch (Exception ex) { ex.printStackTrace(); } } } } 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.亿华云








