Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. package vso.models;
  2.  
  3. import java.sql.Connection; // a connection to the DB
  4. import java.sql.DriverManager; // handles communication with the DB
  5. import java.sql.ResultSet; // a table of rows generated from an SQL query
  6. import java.sql.Statement; // an SQL statement for the DB to execute
  7. import java.util.ArrayList;
  8. import java.util.Date;
  9.  
  10. public class MySQL {
  11.     Connection connection = null;
  12.  
  13.     public MySQL() {
  14.         dataBaseSetUp();
  15.         createTable();
  16.         insertData();
  17.     }
  18.  
  19.     private void dataBaseSetUp() {
  20.         try {
  21.             String connectionURL = "jdbc:mysql://localhost:3306/happyDB?createDatabaseIfNotExist=true";
  22.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  23.             connection = DriverManager.getConnection(connectionURL, "root", "1234");
  24.             if (!connection.isClosed())
  25.                 System.out.println("Successfully connected to " + "MySQL server using TCP/IP...");
  26.         } catch (Exception ex) {
  27.             System.out.println("Unable to connect to database" + ex);
  28.         }
  29.     }
  30.    
  31.     private void createTable() {
  32.         try {
  33.             Statement stmt = connection.createStatement();
  34.             String sql = "CREATE TABLE books (" + "id SERIAL PRIMARY KEY NOT NULL," + "name TEXT NOT NULL,"
  35.                     + "author TEXT NOT NULL," + "date_of_publishing DATE NOT NULL," + "isbn TEXT NOT NULL)";
  36.             stmt.executeUpdate(sql);
  37.             stmt.close();
  38.             System.out.print("Table created");
  39.         } catch (Exception e) {
  40.             System.out.println("Table was not created");
  41.         }
  42.     }
  43.    
  44.     public void insertData() {
  45.         for (int i = 0; i < 10; i++) {
  46.             insertRow("Kniga" + i, "Avtor" + i, i + "");
  47.         }
  48.     }
  49.    
  50.     public void insertRow(String name, String author, String isbn) {
  51.         try {
  52.             Statement stmt = connection.createStatement();
  53.             String sql = "INSERT INTO books (name, author, date_of_publishing, isbn) VALUES('" + name + "', '" + author
  54.                     + "', '" + new java.sql.Date(new Date().getTime()) + "', '" + isbn + "');";
  55.             stmt.executeUpdate(sql);
  56.             stmt.close();
  57.         } catch (Exception e) {
  58.             e.printStackTrace();
  59.         }
  60.     }
  61.    
  62.     public ArrayList<String> readData() {
  63.        
  64.         ArrayList<String> names = new ArrayList<String>();
  65.         try {
  66.             Statement stmt = connection.createStatement();
  67.             ResultSet rs = stmt.executeQuery("SELECT name FROM books;");
  68.  
  69.             while (rs.next()) {
  70.                 //int id = rs.getInt("id");
  71.                 names.add(rs.getString("name"));
  72.                 //String bookAuthor = rs.getString("author");
  73.                 //String bookIsbn = rs.getString("isbn");
  74.                 //System.out.format("%d %s %s %s\n", id, bookName, bookAuthor, bookIsbn);
  75.             }
  76.         } catch (Exception e) {
  77.             e.printStackTrace();
  78.         }
  79.         return names;
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement