关于java:JDBC使用JDBCUtils工具类执行查询操作

4次阅读

共计 560 个字符,预计需要花费 2 分钟才能阅读完成。

// 查问表 test_01 中的所有数据
public class JDBCUtilsSelectTest {public static void main(String[] args) throws SQLException {

        // 1。获取连贯
        Connection conn = JDBCUtils.getConnection();
        // 2。获取语句执行平台,即 Statement 对象
        Statement sqlExecute = JDBCUtils.createStatement(conn);
        // 3。执行 sql 语句
        String sql = "select * from test_01;";
        ResultSet resultSet = sqlExecute.executeQuery(sql);
        // 4。处理结果集
        while(resultSet.next()){int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int age = resultSet.getInt("age");
            System.out.println("id =" + id + ", name =" + name + ", age =" + age);
        }
        // 5。敞开对象
        JDBCUtils.close(conn,sqlExecute,resultSet);
    }
}
正文完
 0