Guest User

Untitled

a guest
Jun 27th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. public static void main(String[] args) {
  2.  
  3. Connection conn = null;
  4. Statement stmt = null;
  5. ResultSet rs = null;
  6.  
  7. try {
  8. String dbURL = "MYSQL";
  9. String username = "produngeon";
  10. String password = "password";
  11.  
  12. Class.forName("com.mysql.jdbc.Driver");
  13.  
  14. conn =
  15. DriverManager.getConnection(dbURL, username, password);
  16.  
  17. stmt = conn.createStatement();
  18.  
  19. if (stmt.execute("select * from testing")) {
  20. rs = stmt.getResultSet();
  21. } else {
  22. System.err.println("select failed");
  23. }
  24. while (rs.next()) {
  25. String entry = rs.getString(1);
  26. System.out.println(entry);
  27. }
  28.  
  29. } catch (ClassNotFoundException ex) {
  30. System.err.println("Failed to load mysql driver");
  31. System.err.println(ex);
  32. } catch (SQLException ex) {
  33. System.out.println("SQLException: " + ex.getMessage());
  34. System.out.println("SQLState: " + ex.getSQLState());
  35. System.out.println("VendorError: " + ex.getErrorCode());
  36. } finally {
  37. if (rs != null) {
  38. try {
  39. rs.close();
  40. } catch (SQLException ex) { /* ignore */ }
  41. rs = null;
  42. }
  43. if (stmt != null) {
  44. try {
  45. stmt.close();
  46. } catch (SQLException ex) { /* ignore */ }
  47. stmt = null;
  48. }
  49. if (conn != null) {
  50. try {
  51. conn.close();
  52. } catch (SQLException ex) { /* ignore */ }
  53. conn = null;
  54. }
  55. }
  56. }
Add Comment
Please, Sign In to add comment