Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class SQLDatabase {
  4.     private Connection connect;
  5.     private Statement statement;
  6.     private ResultSet resultSet;
  7.     private String url;
  8.     private String userName;
  9.     private String password;
  10.  
  11.     public SQLDatabase(String database, String user, String passwd)
  12.             throws InstantiationException, IllegalAccessException,
  13.             ClassNotFoundException, SQLException {
  14.         url = "jdbc:mysql://195.56.52.249/" + database;
  15.         userName = user;
  16.         password = passwd;
  17.         connect();
  18.     }
  19.  
  20.     private void connect() throws InstantiationException,
  21.             IllegalAccessException, ClassNotFoundException, SQLException {
  22.         Class.forName("com.mysql.jdbc.Driver").newInstance();
  23.         connect = DriverManager.getConnection(url, userName, password);
  24.         System.out.println("Connected to database.");
  25.     }
  26.  
  27.     public void readDatabase() throws SQLException {
  28.         statement = connect.createStatement();
  29.         resultSet = statement.executeQuery("SELECT * FROM test");
  30.         getValues(resultSet);
  31.         getMetaData(resultSet);
  32.     }
  33.  
  34.     private void getMetaData(ResultSet resultSet) throws SQLException {
  35.         System.out.println("The columns in the table are: ");
  36.         System.out.println("Table: " + resultSet.getMetaData().getTableName(1));
  37.         for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
  38.             System.out.println("Column " + i + " "
  39.                     + resultSet.getMetaData().getColumnName(i));
  40.         }
  41.     }
  42.  
  43.     private void getValues(ResultSet resultSet) throws SQLException {
  44.         while (resultSet.next()) {
  45.             int ID = resultSet.getInt("ID");
  46.             String name = resultSet.getString("Nev");
  47.             String cim = resultSet.getString("Cim");
  48.             System.out.println("ID: " + ID);
  49.             System.out.println("Nev: " + name);
  50.             System.out.println("Cim: " + cim);
  51.         }
  52.     }
  53.  
  54.     public static void main(String[] args) {
  55.         try {
  56.             SQLDatabase db = new SQLDatabase("test", "prog", "prog");
  57.             db.readDatabase();
  58.         } catch (InstantiationException e) {
  59.             e.printStackTrace();
  60.         } catch (IllegalAccessException e) {
  61.             e.printStackTrace();
  62.         } catch (ClassNotFoundException e) {
  63.             e.printStackTrace();
  64.         } catch (SQLException e) {
  65.             System.err.println("Error message: " + e.getMessage());
  66.             System.err.println("Error number: " + e.getErrorCode());
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement