Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. package GranssnittOchForteckning;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.ResultSetMetaData;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. /*
  11. * To change this license header, choose License Headers in Project Properties.
  12. * To change this template file, choose Tools | Templates
  13. * and open the template in the editor.
  14. */
  15. /**
  16. *
  17. * @author augustforsman
  18. */
  19. public class dataBas {
  20.  
  21. Connection dbConnection = null;
  22. Statement statement = null;
  23. ResultSet rs = null;
  24.  
  25. public void connect() {
  26.  
  27. String url = "jdbc:mysql://atlantis.informatik.umu.se/svph1517_db2_ht2016";
  28. String user = "svph1517_2016264";
  29. String password = "bavedaqad";
  30.  
  31. try {
  32. Class.forName("com.mysql.jdbc.Driver").newInstance();
  33. dbConnection = DriverManager.getConnection(url, user, password);
  34. } catch (Exception e) {
  35. System.err.println("MYSQL:s JDBC-drivrutiner kunde INTE hittas.");
  36. }
  37. System.out.println("Uppkoppling mot databasen öppnad.");
  38. }
  39.  
  40. public void select() {
  41.  
  42. try {
  43.  
  44. String strSql = "SELECT * FROM ex2_skivor";
  45. statement = dbConnection.createStatement();
  46. rs = statement.executeQuery(strSql);
  47. ResultSetMetaData rsmd = rs.getMetaData();
  48. int columnsNumber = rsmd.getColumnCount();
  49.  
  50. // Iterate through the data in the result set and display it.
  51. while (rs.next()) {
  52. //Print one row
  53. for (int i = 1; i <= columnsNumber; i++) {
  54.  
  55. System.out.print(rs.getString(i) + " "); //Print one element of a row
  56.  
  57. }
  58.  
  59. System.out.println();//Move to the next line to print the next row.
  60.  
  61. }
  62.  
  63. } catch (SQLException ex) {
  64. System.err.println("Ett fel har uppstått: " + ex.toString());
  65. }
  66.  
  67. }
  68.  
  69. public void close() {
  70.  
  71. try {
  72. if (rs != null) {
  73. rs.close();
  74. }
  75. if (statement != null) {
  76. statement.close();
  77. }
  78. if (dbConnection != null) {
  79. dbConnection.close();
  80. }
  81. } catch (SQLException ex) {
  82. System.err.println("Ett fel har uppstått: " + ex.toString());
  83. }
  84. System.out.println("Uppkoppling mot databasen stängd.");
  85. }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement