Guest User

Untitled

a guest
Nov 6th, 2017
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.ArrayList;
  3.  
  4. public class SQLSelectRpvs {
  5. private Connection connect = null;
  6. private Statement statement = null;
  7. private PreparedStatement preparedStatement = null;
  8. private ResultSet resultSet = null;
  9.  
  10.  
  11. public ArrayList<Integer> readDataBase() throws Exception {
  12. try {
  13. // toto načíta MySQL ovládač, každá DB má vlastný ovládač
  14. Class.forName("com.mysql.jdbc.Driver");
  15.  
  16. // Nastavenie spojenia s DB
  17. connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/?user=root&password=8671");
  18. statement = connect.createStatement();
  19.  
  20. // pre vyobrazenie
  21. resultSet = statement.executeQuery("SELECT id from tssu.faktura tssu inner join rpvs.seller rpvs where tssu.ico = rpvs.ico");
  22. return writeResultSet(resultSet);
  23.  
  24.  
  25. } catch (Exception e) {
  26. throw e;
  27. } finally {
  28. close();
  29. }
  30.  
  31. }
  32.  
  33.  
  34. //vypisanie prvkov databazy
  35. private ArrayList<Integer> writeResultSet(ResultSet resultSet) throws SQLException {
  36. // ResultSet is initially before the first data set
  37. ArrayList<Integer> ico = new ArrayList<Integer>();
  38. while (resultSet.next()) {
  39. // It is possible to get the columns via name
  40. // also possible to get the columns via the column number
  41. // which starts at 1
  42. // e.g. resultSet.getSTring(2);
  43. ico.add(resultSet.getInt("id"));
  44. }
  45. return ico;
  46. }
  47.  
  48. // You need to close the resultSet
  49. private void close() {
  50. try {
  51. if (resultSet != null) {
  52. resultSet.close();
  53. }
  54.  
  55. if (statement != null) {
  56. statement.close();
  57. }
  58.  
  59. if (connect != null) {
  60. connect.close();
  61. }
  62. } catch (Exception e) {
  63.  
  64. }
  65. }
  66.  
  67. }
Add Comment
Please, Sign In to add comment