JDBC-使用JDBCUtils工具類執行查詢操做

// 查詢表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);
    }
}
相關文章
相關標籤/搜索