标签:获取 close http except local 执行sql ext exec exe
public class JDBC { public static void main(String[] args) { Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { // 1.注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); // 2.创建连接 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=123456"); // 3.创建执行SQL语句的对象 statement = connection.createStatement(); // 4.执行SQL语句并返回结果集 resultSet = statement.executeQuery("select * from user;"); // 5.遍历输出 while (resultSet.next()) { System.out.print(resultSet.getObject(1) + " ");//按照列的索引获取 System.out.print(resultSet.getObject(2) + " "); System.out.print(resultSet.getObject(3) + " "); System.out.print(resultSet.getObject(4) + " "); System.out.println(resultSet.getObject(5) + " "); } } catch (Exception e) { // TODO: handle exception } finally { //6.关闭连接 if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
这样做的不好处是,如果我要进增删改等操作,每次都需要写建立连接和释放连接的代码。所以,可以考虑用封装的思想把建立连接和释放连接写到工具类里面去。
标签:获取 close http except local 执行sql ext exec exe
原文地址:https://www.cnblogs.com/pilgrimL/p/10200478.html