Advertisement
Guest User

Untitled

a guest
Dec 8th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. package lab8.zad2;
  2.  
  3. import java.sql.*;
  4.  
  5. import static java.lang.System.out;
  6.  
  7. public class DB {
  8.  
  9. public static ResultSet searchAuthor(String authorsBooks, String parametr) throws SQLException {
  10. ResultSet rs;
  11. Connection c = DriverManager.getConnection("jdbc:mysql://mysql.agh.edu.pl/amaczyn", "amaczyn", "gt3W6btDC0926rd7");
  12. if(parametr.equals("author")) {
  13. PreparedStatement stmt = c.prepareStatement("SELECT * FROM books WHERE author LIKE ? ;");
  14. stmt.setString(1, "%" +authorsBooks +"%");
  15. rs = stmt.executeQuery();
  16. return rs;
  17. }
  18. else {
  19. PreparedStatement stmt = c.prepareStatement("SELECT * FROM books WHERE isbn=? ;");
  20. stmt.setString(1, authorsBooks);
  21. rs = stmt.executeQuery();
  22. return rs;
  23. }
  24. }
  25.  
  26. public static boolean insertBook(String isbn, String title, String author, String year) throws SQLException {
  27. Connection c = DriverManager.getConnection("jdbc:mysql://mysql.agh.edu.pl/amaczyn", "amaczyn", "gt3W6btDC0926rd7");
  28. PreparedStatement statement = c.prepareStatement("INSERT INTO books VALUES (?, ?, ?, ?)");
  29. statement.setString(1, isbn);
  30. statement.setString(2, title);
  31. statement.setString(3, author);
  32. statement.setString(4, year);
  33. statement.executeUpdate();
  34.  
  35. return true;
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement