Advertisement
Guest User

Untitled

a guest
Jan 25th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.sql.PreparedStatement;
  9.  
  10. public class SearchDAO {
  11.  
  12. List<BookInformation> exactdata = new ArrayList<BookInformation> ();
  13. List<BookInformation> partialdata = new ArrayList<BookInformation> ();
  14.  
  15. boolean resultfound = false;
  16.  
  17. public void Search(BookInformation data) {
  18.  
  19. final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  20. final String DB_URL = "jdbc:mysql://localhost/book";
  21.  
  22. // Database credentials
  23. final String USER = "root";
  24. final String PASS = "1234";
  25.  
  26. Connection conn = null;
  27. Statement stmt = null;
  28. try{
  29. //STEP 2: Register JDBC driver
  30. // Class.forName("com.mysql.jdbc.Driver");
  31. Class.forName("com.mysql.jdbc.Driver");
  32. //STEP 3: Open a connection
  33. System.out.println("Connecting to database...");
  34. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  35.  
  36. //STEP 4: Execute a query
  37. System.out.println("Creating statement...");
  38. //System.out.println("check"+data.Author);
  39.  
  40. stmt = conn.createStatement();
  41. String sql;
  42. //
  43. // sql = "SELECT bookinfo.book_id,bookinfo.Book_Title,bookinfo.Date_Issue, "
  44. // + "group_concat(distinct(Author_Name)) as Author_Name,group_concat(distinct(Trans_Name)) as Trans_Name,genreinfo.Genre_Name "
  45. // + "from bookinfo left join translatorinfo on bookinfo.Book_Id = translatorinfo.Book_Id "
  46. // + "left join authorinfo on bookinfo.Book_Id = authorinfo.Book_Id "
  47. // + "left join genreinfo on bookinfo.Book_Id = genreinfo.Book_Id "
  48. // + "where Book_Status = 1 "
  49. // + "group by bookinfo.Book_Id";
  50.  
  51. //sql = "SELECT bookinfo.book_id from bookinfo where book_id = " + data.getId();
  52.  
  53. sql = "SELECT bookinfo.book_id "
  54. + "from bookinfo "
  55. + "left join translatorinfo on bookinfo.Book_Id = translatorinfo.Book_Id "
  56. + "left join authorinfo on bookinfo.Book_Id = authorinfo.Book_Id "
  57. + "left join genreinfo on bookinfo.Book_Id = genreinfo.Book_Id "
  58. + "where Book_Status = 1 " ;
  59.  
  60.  
  61. if(data.getTitle().trim().equals("") == false){
  62.  
  63. sql += " or bookinfo.book_name = '" + data.getTitle().trim() + "' " ;
  64. }
  65. if(data.getDmy().trim().equals("") == false){
  66.  
  67. sql += " or bookinfo.date_issue = '" + data.getDmy().trim() + "' " ;
  68. }
  69. if(data.getDonor().trim().equals("") == false){
  70.  
  71. sql += " or bookinfo.donor_name = '" + data.getDonor().trim() + "' " ;
  72. }
  73. if(data.getGenre().trim().equals("") == false){
  74.  
  75. sql += " or genreinfo.genre_name = '" + data.getGenre().trim() + "' " ;
  76. }
  77.  
  78. if(data.getauthorlist().size() >= 1){
  79. sql += " or authorinfo.author_name IN (" ;
  80. String tempitem = "";
  81. for(String item : data.getauthorlist()){
  82. tempitem += ",'" + item.trim() + "'";
  83. }
  84.  
  85. tempitem = tempitem.substring(1);
  86.  
  87. sql += tempitem + ") " ;
  88.  
  89. }
  90.  
  91. if(data.gettranslatorlist().size() >= 1){
  92. sql += " or translatorinfo.trans_name IN (" ;
  93. String tempitem = "";
  94. for(String item : data.gettranslatorlist()){
  95. tempitem += ",'" + item.trim() + "'";
  96. }
  97.  
  98. tempitem = tempitem.substring(1);
  99.  
  100. sql += tempitem + ") " ;
  101.  
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108. ResultSet rs = stmt.executeQuery(sql);
  109. System.out.println(sql);
  110.  
  111. List<String>BookID = new ArrayList<String>();
  112. while(rs.next()){
  113. BookID.add(rs.getString("book_id"));
  114. }
  115.  
  116. String partialID = "";
  117. for(String item : BookID){
  118. partialID += " or bookinfo.Book_Id = " + item;
  119.  
  120. }
  121.  
  122. partialID = partialID.substring(3);
  123.  
  124.  
  125. sql = "SELECT bookinfo.book_id,bookinfo.Book_Title,bookinfo.Date_Issue, "
  126. + "group_concat(distinct(Author_Name)) as Author_Name,group_concat(distinct(Trans_Name)) as Trans_Name,genreinfo.Genre_Name "
  127. + "from bookinfo left join translatorinfo on bookinfo.Book_Id = translatorinfo.Book_Id "
  128. + "left join authorinfo on bookinfo.Book_Id = authorinfo.Book_Id "
  129. + "left join genreinfo on bookinfo.Book_Id = genreinfo.Book_Id "
  130. + "where " + partialID
  131. + " group by bookinfo.Book_Id";
  132.  
  133. rs = stmt.executeQuery(sql);
  134.  
  135.  
  136. while (rs.next()) {
  137. BookInformation t = new BookInformation();
  138. t.Id = rs.getString("Book_Id");
  139. t.Title = rs.getString("Book_Title");
  140. t.Author = rs.getString("Author_Name");
  141. t.Translator = rs.getString("Trans_Name");
  142. t.Genre = rs.getString("Genre_Name");
  143. t.Dmy = rs.getString("Date_Issue");
  144.  
  145. String checkauthor = rs.getString("Author_Name");
  146. List<String> authorlist = new ArrayList<String>();
  147. if(checkauthor != null)
  148. {
  149. String [] authortemp = rs.getString("Author_Name").split(",");
  150. for(String item: authortemp)
  151. {
  152. if(item.trim().equals("") == false)
  153. {
  154. authorlist.add(item);
  155. }
  156. }
  157. }
  158. String checktranslator = rs.getString("Trans_Name");
  159. List<String> translatorlist = new ArrayList<String>();
  160. if(checktranslator != null)
  161. {
  162. String [] translatortemp = rs.getString("Trans_Name").split(",");
  163. for(String item: translatortemp)
  164. {
  165. if(item.trim().equals("") == false)
  166. {
  167. translatorlist.add(item);
  168. }
  169. }
  170. }
  171. t.authorlist = authorlist;
  172. t.translatorlist = translatorlist;
  173. // System.out.println(rs.getString("Book_Id"));
  174. // System.out.println(rs.getString("Book_Title"));
  175. // System.out.println(rs.getString("Author_Name"));
  176. // System.out.println(rs.getString("Trans_Name"));
  177. // System.out.println(rs.getString("Genre_Name"));
  178. // System.out.println(rs.getString("Date_Issue"));
  179. partialdata.add(t);
  180. }
  181.  
  182.  
  183. if(partialdata.size() >= 1 || exactdata.size() >= 1){
  184. resultfound = true;
  185. }
  186.  
  187. rs.close();
  188.  
  189.  
  190.  
  191. stmt.close();
  192. conn.close();
  193. }catch(SQLException se){
  194. //Handle errors for JDBC
  195. se.printStackTrace();
  196. }catch(Exception e){
  197. //Handle errors for Class.forName
  198. e.printStackTrace();
  199. }finally{
  200. //finally block used to close resources
  201. try{
  202. if(stmt!=null)
  203. stmt.close();
  204. }catch(SQLException se2){
  205. }// nothing we can do
  206. try{
  207. if(conn!=null)
  208. conn.close();
  209. }catch(SQLException se){
  210. se.printStackTrace();
  211. }//end finally try
  212. }//end try
  213.  
  214.  
  215. }
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement