Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.ArrayList;
- public class SQLSelectRpvs {
- private Connection connect = null;
- private Statement statement = null;
- private PreparedStatement preparedStatement = null;
- private ResultSet resultSet = null;
- public ArrayList<Integer> readDataBase() throws Exception {
- try {
- // toto načíta MySQL ovládač, každá DB má vlastný ovládač
- Class.forName("com.mysql.jdbc.Driver");
- // Nastavenie spojenia s DB
- connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/?user=root&password=8671");
- statement = connect.createStatement();
- // pre vyobrazenie
- resultSet = statement.executeQuery("SELECT id from tssu.faktura tssu inner join rpvs.seller rpvs where tssu.ico = rpvs.ico");
- return writeResultSet(resultSet);
- } catch (Exception e) {
- throw e;
- } finally {
- close();
- }
- }
- //vypisanie prvkov databazy
- private ArrayList<Integer> writeResultSet(ResultSet resultSet) throws SQLException {
- // ResultSet is initially before the first data set
- ArrayList<Integer> ico = new ArrayList<Integer>();
- while (resultSet.next()) {
- // It is possible to get the columns via name
- // also possible to get the columns via the column number
- // which starts at 1
- // e.g. resultSet.getSTring(2);
- ico.add(resultSet.getInt("id"));
- }
- return ico;
- }
- // You need to close the resultSet
- private void close() {
- try {
- if (resultSet != null) {
- resultSet.close();
- }
- if (statement != null) {
- statement.close();
- }
- if (connect != null) {
- connect.close();
- }
- } catch (Exception e) {
- }
- }
- }
Add Comment
Please, Sign In to add comment