Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. public class SelectExample {
  2. public static void main(String[] args) throws ClassNotFoundException {
  3. Connection connection = null;
  4. try {
  5. // load driver
  6. Class.forName("com.mysql.jdbc.Driver");
  7. // tao ket noi
  8. connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "");
  9. // truy van
  10. Statement statement = connection.createStatement();
  11. ResultSet resultSet = statement.executeQuery("SELECT * from student");
  12. while(resultSet.next()) {
  13. System.out.println(resultSet.getString("student_code"));
  14. System.out.println(resultSet.getString("name"));
  15. System.out.println(resultSet.getString("class"));
  16. System.out.println(resultSet.getString("faculty"));
  17. }
  18. } catch (SQLException e) {
  19. System.err.println(e);
  20. }finally {
  21. // dong ket noi
  22. if(connection != null)
  23. try {
  24. connection.close();
  25. } catch (SQLException e) {
  26. System.err.println(e);
  27. }
  28. }
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement