Guest User

Untitled

a guest
Sep 5th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.73 KB | None | 0 0
  1. import java.text.*;
  2. import java.util.*;
  3. import java.io.*;
  4. import java.sql.*;
  5.  
  6. public class Methods_version5 {
  7.  
  8. /************************************************************************/
  9. public static void loadJDBCDriverClass() {
  10.  
  11. //Load JDBC driver class
  12. try {
  13. Class.forName("org.postgresql.Driver");
  14. } catch (ClassNotFoundException cnfe) {
  15. System.err.println("\nAn error has occured while loading the "
  16. + "JDBC driver:\n");
  17. cnfe.printStackTrace();
  18. System.exit(1);
  19. }
  20. System.out.println("\nLoading drivers for JDBC...\n");
  21.  
  22. }
  23. //***********************************************************************/
  24. public static Connection connectPostgreSQLDatabase() {
  25.  
  26. //Connect to PostgreSQL database
  27. Connection c = null;
  28.  
  29. try {
  30. c = DriverManager.getConnection(
  31. "jdbc:postgresql://localhost/",
  32. "java","java");
  33. } catch (SQLException connection_failed) {
  34. System.out.println("Unable to establish a connection to the "
  35. + "database.\n");
  36. System.exit(1);
  37. }
  38.  
  39. return c;
  40. }
  41. //***********************************************************************/
  42. public static Statement checkStatement() {
  43.  
  44. //Check the connection
  45. Statement s = null;
  46. try {
  47. s = connectPostgreSQLDatabase().createStatement();
  48. } catch (SQLException se) {
  49. System.out.println("An exception error occured while creating a"
  50. + " statement. Probably becuase the connection is lost.\n");
  51. se.printStackTrace();
  52. System.exit(1);
  53. }
  54.  
  55. return s;
  56. }
  57. //***********************************************************************/
  58. public static ResultSet runSQL(String query) {
  59.  
  60. ResultSet resultat = null;
  61.  
  62. try {
  63. resultat = checkStatement().executeQuery(query);
  64. } catch (SQLException query_failed) {
  65. System.out.println("Felaktigt formulerad SQL-sats");
  66. System.exit(1);
  67. }
  68.  
  69. return resultat;
  70. }
  71. //***********************************************************************/
  72. public static void addBorrower() {
  73.  
  74. boolean addUser=true;
  75. boolean loop;
  76. int index;
  77. int hasCardNrBeenChecked;
  78. ResultSet rs = null;
  79. Scanner in = new Scanner(System.in);
  80. String databaseCardNr="110";
  81. String inputCardNr;
  82. String answer;
  83. String borrowerName;
  84. String streetAddress;
  85. String postalAddress;
  86.  
  87.  
  88. // Add a borrower
  89. while (addUser==true) {
  90.  
  91. loop=true;
  92.  
  93. System.out.println("\n\n\n\n\n#####################################"
  94. + "#############################\n\nAdd a borrower\n\n#####"
  95. + "########################################################"
  96. + "#####");
  97.  
  98. System.out.print("\nInput a card number: ");
  99. inputCardNr = in.nextLine();
  100.  
  101.  
  102. //Makes sure thar the chosen card number is unique
  103. while (loop==true) {
  104. index = 0;
  105. rs = null;
  106. hasCardNrBeenChecked = 0;
  107.  
  108. try {
  109. rs = Methods_version5.checkStatement().executeQuery(
  110. "SELECT librarycardno FROM LTAGARE;");
  111. } catch (SQLException se) {
  112. System.out.println("An exception error has occured while "
  113. + "executing the query.\n");
  114. se.printStackTrace();
  115. System.exit(1);
  116. }
  117.  
  118. try {
  119. while (rs.next()) {
  120. databaseCardNr = rs.getString(1);
  121. if (databaseCardNr.equals(inputCardNr)) {
  122. System.out.print("\nThe card number " + inputCardNr
  123. + " already exists.\nPlease input another "
  124. + "card number: ");
  125. inputCardNr = in.nextLine();
  126. loop = true;
  127. hasCardNrBeenChecked++;
  128. }
  129. }
  130. } catch (SQLException se) {
  131. System.out.println("An exception error has occured.\n");
  132. se.printStackTrace();
  133. System.exit(1);
  134. }
  135. if (hasCardNrBeenChecked==0) {
  136. loop = false;
  137. }
  138. }
  139.  
  140.  
  141. System.out.print("\nInput a name: ");
  142. borrowerName = in.nextLine();
  143. while (borrowerName.equals("")) {
  144. System.out.print("\nInput a name: " );
  145. borrowerName = in.nextLine();
  146. }
  147.  
  148. System.out.print("\nInput a street address: ");
  149. streetAddress = in.nextLine();
  150. while (streetAddress.equals("")) {
  151. System.out.print("\nInput a street address: " );
  152. streetAddress = in.nextLine();
  153. }
  154.  
  155. System.out.print("\nInput a postal address: ");
  156. postalAddress = in.nextLine();
  157. while (postalAddress.equals("")) {
  158. System.out.print("\nInput a postal address: " );
  159. postalAddress = in.nextLine();
  160. }
  161.  
  162. //Prints SQL statement on screen for manual error check
  163. System.out.println(
  164. "\nINSERT INTO Ltagare (librarycardno, "
  165. + "name, "
  166. + "streetaddress, "
  167. + "postaladdress) "
  168. + "VALUES (" + inputCardNr + ", "
  169. + "'" + borrowerName + "', "
  170. + "'" + streetAddress + "', "
  171. + "'" + postalAddress + "');\n");
  172.  
  173. //Inserts the new data into the database
  174. ResultSet resultat = null;
  175. try {
  176. resultat = Methods_version5.checkStatement().executeQuery(
  177. "INSERT INTO Ltagare (librarycardno, "
  178. + "name, "
  179. + "streetaddress, "
  180. + "postaladdress) "
  181. + "VALUES (" + inputCardNr + ", "
  182. + "'" + borrowerName + "', "
  183. + "'" + streetAddress + "', "
  184. + "'" + postalAddress + "');");
  185. } catch (SQLException query_failed) {
  186. ;
  187. //This doesn't work, all feedback seems to be treated as errors
  188. //System.out.println("Incorrect SQL statement");
  189. //System.exit(1);
  190. }
  191.  
  192. //Ask the user if he wants to add another borrower
  193. System.out.print("\nDo you want to add another borrower? (y/n): ");
  194. answer = in.nextLine();
  195. while (!answer.equalsIgnoreCase("y") &&
  196. !answer.equalsIgnoreCase("n")) {
  197. System.out.print("\nDo you want to add another borrower? "
  198. + "(y/n): ");
  199. answer = in.nextLine();
  200. }
  201. if (answer.equalsIgnoreCase("y")) {
  202. addUser = true;
  203. } else {
  204. addUser = false;
  205. }
  206. }
  207.  
  208. }
  209.  
  210. //***********************************************************************/
  211. //kontrollerar att kontrolltecknet
  212. // i ISBN-numret är korrekt
  213. public static boolean isIsbn(String s) {
  214.  
  215. String kSiffra, nio; //kSiffra=kontrolltecknet som en String
  216. int sum, koll; // koll = kontrolltecknet som en int
  217.  
  218. nio = s.replaceAll("-","");
  219.  
  220. //nio innehåller nu 10 tecken, 9 siffror + kontrolltecknet.
  221. sum = 0;
  222. for (int i = 1;i<=9;i++)
  223. sum = sum + i*Integer.parseInt(nio.substring(i-1,i));
  224.  
  225. koll = sum%11; // koll = kontrollvärdet som heltal
  226. if (koll == 10)
  227. kSiffra = "X";
  228. else
  229. kSiffra = String.valueOf(koll);
  230.  
  231. // Kontrollera kontrolltecknet och returnera true el false
  232. return kSiffra.equals(s.substring(12));
  233.  
  234. }
  235. }
Add Comment
Please, Sign In to add comment