Advertisement
Guest User

Untitled

a guest
Jan 28th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class HelloWorld {
  4.  
  5. public static void main(String[] args)
  6. {
  7. // TODO Auto-generated method stub
  8.  
  9. System.out.println("Hello world!");
  10. System.out.println(System.getProperty("java.class.path"));
  11. Connection connection = null;
  12. Statement stmt = null;
  13. ResultSet rs = null;
  14. ResultSet rs2 = null;
  15. PreparedStatement query = null;
  16.  
  17. try {
  18.  
  19. System.out.println("Create the driver instance.<br>");
  20. Class.forName("com.mysql.jdbc.Driver").newInstance();
  21.  
  22. System.out.println("Get the connection.<br>");
  23. connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/philstestschema", "root", "root1234");
  24. // query = connection.prepareStatement( "SELECT * FROM philstable");
  25. stmt = connection.createStatement();
  26. rs = stmt.executeQuery("SELECT username, firstname, surname FROM philstable");
  27.  
  28. System.out.println("Made it past the query");
  29.  
  30. while (rs.next())
  31. {
  32. String username = rs.getString("username");
  33. String firstname = rs.getString("FIRSTNAME");
  34. String surname = rs.getString("SURNAME");
  35.  
  36. System.out.println(username + "\t" + firstname + "\t" + surname);
  37. }
  38.  
  39. } catch (Exception e)
  40. {
  41. System.out.println("Phils Exception Handler");
  42. System.out.println(e.toString()+"<br>");
  43. }
  44.  
  45. finally {
  46. // it is a good idea to release
  47. // resources in a finally{} block
  48. // in reverse-order of their creation
  49. // if they are no-longer needed
  50.  
  51. if (rs != null) {
  52. try {
  53. rs.close();
  54. } catch (SQLException sqlEx) { } // ignore
  55.  
  56. rs = null;
  57. }
  58.  
  59. if (stmt != null) {
  60. try {
  61. stmt.close();
  62. } catch (SQLException sqlEx) { } // ignore
  63.  
  64. stmt = null;
  65. }
  66. }
  67. }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement