Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. package pckg1;
  2.  
  3. import java.sql.*;
  4.  
  5. public class DB{
  6. private Connection conn = null;
  7. private Statement stmt = null;
  8. private ResultSet rs = null;
  9.  
  10. public void connect(){
  11. try {
  12. Class.forName("com.mysql.jdbc.Driver").newInstance();
  13. conn =
  14. DriverManager.getConnection("jdbc:mysql://mysql.agh.edu.pl/tomaada","tomaada","R2jqv8np");
  15. } catch (SQLException ex) {
  16. // handle any errors
  17. System.out.println("SQLException: " + ex.getMessage());
  18. System.out.println("SQLState: " + ex.getSQLState());
  19. System.out.println("VendorError: " + ex.getErrorCode());
  20. }catch(Exception e){e.printStackTrace();}
  21. }
  22. public void listNames(){
  23. try {
  24. connect();
  25. stmt = conn.createStatement();
  26. rs = stmt.executeQuery("SELECT * FROM books");
  27.  
  28. while(rs.next()){
  29. for (int i=1;i<5;i++){
  30. String name = rs.getString(i)+' ';
  31. System.out.print(name );
  32. }
  33. System.out.println();
  34. }
  35. }catch (SQLException ex){
  36. // handle any errors
  37.  
  38. }finally {
  39. // zwalniamy zasoby, które nie będą potrzebne
  40. if (rs != null) {
  41. try {
  42. rs.close();
  43. } catch (SQLException sqlEx) { } // ignore
  44. rs = null;
  45. }
  46.  
  47. if (stmt != null) {
  48. try {
  49. stmt.close();
  50. } catch (SQLException sqlEx) { } // ignore
  51.  
  52. stmt = null;
  53. }
  54. }
  55. }
  56.  
  57. public void findNames(String pattern){
  58. try {
  59. connect();
  60. stmt = conn.createStatement();
  61. rs = stmt.executeQuery("SELECT * FROM books");
  62. boolean found=false;
  63. while(rs.next()){
  64. if (pattern.equals(rs.getString(1)) || pattern.equals(rs.getString(3)) ){ //1-isbn, 3-autor
  65. for(int j=1;j<5;j++){
  66. System.out.print(rs.getString(j)+' '); //wypisanie pozostalych informacji
  67. }
  68. System.out.println();
  69. found = true;
  70. }
  71. }
  72. if (!found){
  73. System.out.println("Nie znaleziono rekordu.");
  74. }
  75. }catch (SQLException ex){
  76. // handle any errors
  77.  
  78. }finally {
  79. // zwalniamy zasoby, które nie będą potrzebne
  80. if (rs != null) {
  81. try {
  82. rs.close();
  83. } catch (SQLException sqlEx) { } // ignore
  84. rs = null;
  85. }
  86.  
  87. if (stmt != null) {
  88. try {
  89. stmt.close();
  90. } catch (SQLException sqlEx) { } // ignore
  91.  
  92. stmt = null;
  93. }
  94. }
  95. }
  96.  
  97. public void deleteNames(String pattern){
  98. try {
  99. connect();
  100. stmt = conn.createStatement();
  101. stmt.executeUpdate(
  102. "DELETE FROM books WHERE isbn='"+pattern+"' OR author='"+pattern+"'");
  103. System.out.println("Dane usnięte z bazy");
  104. }catch (SQLException ex){
  105. // handle any errors
  106. }finally {
  107. // zwalniamy zasoby, które nie będą potrzebne
  108. if (rs != null) {
  109. try {
  110. rs.close();
  111. } catch (SQLException sqlEx) { } // ignore
  112. rs = null;
  113. }
  114.  
  115. if (stmt != null) {
  116. try {
  117. stmt.close();
  118. } catch (SQLException sqlEx) { } // ignore
  119.  
  120. stmt = null;
  121. }
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement