Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. public static void dbConnect() {
  2. Connection dbConnection = null;
  3. Statement statement = null;
  4. ResultSet rs = null;
  5.  
  6. try {
  7. Class.forName("com.mysql.jdbc.Driver").newInstance();
  8. System.out.println("MYSQL:s JDBC-drivrutiner hittades. ");
  9. } catch (Exception e) {
  10. System.err.println("MYSQL:s JDBC-drivrutiner kunde INTE hittas.");
  11. }
  12.  
  13. //Byt ut databasnamn mot den databas ni vill koppla er mot.
  14. String url = "jdbc:mysql://atlantis.informatik.umu.se/svph1510_db_ht2016";
  15. String user = "svph1510_2016264";
  16. String password = "yryrevasa";
  17.  
  18. }
  19.  
  20. public static void dbClose() {
  21. Connection dbConnection = null;
  22. Statement statement = null;
  23. ResultSet rs = null;
  24.  
  25. try {
  26. if (rs != null) {
  27. rs.close();
  28. }
  29. if (statement != null) {
  30. statement.close();
  31. }
  32. if (dbConnection != null) {
  33. dbConnection.close();
  34. }
  35. } catch (SQLException ex) {
  36. System.err.println("Ett fel har uppstått: " + ex.toString());
  37. }
  38. System.out.println("Stängd");
  39. }
  40.  
  41. public static void dbInsert(){
  42.  
  43. Connection dbConnection = null;
  44. Statement statement = null;
  45. ResultSet rs = null;
  46. String url = "jdbc:mysql://atlantis.informatik.umu.se/svph1510_db_ht2016";
  47. String user = "svph1510_2016264";
  48. String password = "yryrevasa";
  49.  
  50.  
  51. try {
  52. dbConnection = DriverManager.getConnection(url, user, password);
  53. String strSql = "SELECT flyg, datum FROM avgang ORDER BY flyg";
  54. statement = dbConnection.createStatement();
  55. rs = statement.executeQuery(strSql);
  56. while (rs.next()) {
  57. System.out.println(rs.getString("flyg") + ", " + rs.getString("datum"));
  58. }
  59.  
  60. } catch (SQLException ex) {
  61. System.err.println("Ett fel har uppstått: " + ex.toString());
  62. }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement