Advertisement
BorislavaYordanova

DataBase

May 5th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. package library;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.sql.Date;
  9.  
  10. public class DataBaseExporter {
  11.  
  12.     private ResultSet result = null;
  13.     private Statement statement = null;
  14.     private Connection connection = null;
  15.  
  16.     public void close() throws SQLException {
  17.         if (!result.equals(null))
  18.             result.close();
  19.         if (!statement.equals(null))
  20.             statement.close();
  21.         if (!connection.equals(null))
  22.             connection.close();
  23.     }
  24.  
  25.     public void executeQuery(String arg0) throws SQLException, ClassNotFoundException {
  26.         if (arg0.isEmpty())
  27.             return;
  28.  
  29.         Class.forName("com.mysql.jdbc.Driver");
  30.         connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "123456");
  31.         statement = connection.createStatement();
  32.         result = statement.executeQuery(arg0);
  33.     }
  34.  
  35.     public boolean hasNext() throws SQLException {
  36.         if (!result.equals(null))
  37.             return result.next();
  38.  
  39.         return false;
  40.     }
  41.  
  42.     public String getAuthor() throws SQLException {
  43.         if (!result.equals(null))
  44.             return result.getString("author");
  45.  
  46.         return null;
  47.     }
  48.  
  49.     public String getFormat() throws SQLException {
  50.         if (!result.equals(null))
  51.             return result.getString("format");
  52.  
  53.         return null;
  54.     }
  55.  
  56.     public String[] getKeywordsArray() throws SQLException {
  57.         if (result.equals(null))
  58.             return null;
  59.  
  60.         String[] keywords = result.getString("keywords").split(",");
  61.  
  62.         if (keywords.length < 0) {
  63.             return null;
  64.         }
  65.  
  66.         return keywords;
  67.     }
  68.  
  69.     public String getKeywords() throws SQLException {
  70.         if (!result.equals(null))
  71.             return result.getString("keywords");
  72.  
  73.         return null;
  74.     }
  75.  
  76.     public String getPublisher() throws SQLException {
  77.         if (!result.equals(null))
  78.             return result.getString("publisher");
  79.  
  80.         return "";
  81.     }
  82.  
  83.     public float getPrice() throws SQLException {
  84.         if (!result.equals(null))
  85.             return result.getFloat("price");
  86.  
  87.         return 0.0f;
  88.     }
  89.  
  90.     public String getTitle() throws SQLException {
  91.         if (!result.equals(null))
  92.             return result.getString("title");
  93.  
  94.         return null;
  95.     }
  96.  
  97.     public int getID() throws SQLException {
  98.         int id = 0;
  99.  
  100.         if (!result.equals(null))
  101.             id = result.getInt("id");
  102.  
  103.         if (id < 0)
  104.             return 0;
  105.  
  106.         return id;
  107.     }
  108.  
  109.     public Date getDate() throws SQLException {
  110.         Date date = null;
  111.  
  112.         if (!result.equals(null)) {
  113.             date = result.getDate("date");
  114.             return date;
  115.         }
  116.  
  117.         return date;
  118.     }
  119.  
  120.     public String getISBN() throws SQLException {
  121.         if (!result.equals(null))
  122.             return result.getString("ISBN");
  123.  
  124.         return null;
  125.     }
  126.  
  127.     public String getName() throws SQLException {
  128.         return result.getString("name");
  129.     }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement