Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Queue;
  3.  
  4. public class Database {
  5. private String brukernavn = "aadneny";
  6. private String passord = "yO20REny";
  7. private String databasedriver = "com.mysql.jdbc.Driver";
  8. private String databasenavn = "jdbc:mysql://mysql.stud.iie.ntnu.no:3306/" + brukernavn + "?user=" + brukernavn + "&password=" + passord;
  9. private Connection forbindelse;
  10. private Statement setning;
  11. private ResultSet res;
  12.  
  13. public Database(){
  14. try {
  15. forbindelse = DriverManager.getConnection(databasenavn);
  16. } catch(Exception e){
  17.  
  18. }
  19. }
  20.  
  21. public void lukkForbindelse(){
  22. try{
  23. forbindelse.close();
  24. } catch (Exception e){
  25.  
  26. }
  27. }
  28.  
  29. public void lukkSetning(){
  30. try{
  31. setning.close();
  32. } catch(Exception e){
  33.  
  34. }
  35. }
  36.  
  37. public void lukkRes(){
  38. try{
  39. res.close();
  40. } catch(Exception e){
  41.  
  42. }
  43. }
  44.  
  45. public boolean regNyBok(Bok nyBok){
  46. try{
  47. forbindelse.setAutoCommit(false);
  48.  
  49. String isbn = nyBok.getIsbn();
  50. String tittel = nyBok.getTittel();
  51. String forfatter = nyBok.getForfatter();
  52. String insertIntoTittel = "insert into boktittel(isbn, forfatter, tittel) values(" + isbn + "," + forfatter +"," + tittel + ")";
  53. String insertIntoEksemplar = "insert into eksemplar(isbn, eks_nr) values (" + isbn + ", 1)";
  54.  
  55. // oppretter setningen
  56. setning = forbindelse.createStatement();
  57.  
  58. // sjekker om ISB finnes
  59. res = setning.executeQuery("SELECT COUNT(*) FROM `boktittel` WHERE isbn LIKE '" + isbn +"'");
  60. int isbnCheck = -1;
  61. while(res.next()){
  62. isbnCheck = res.getInt(isbn);
  63. }
  64.  
  65.  
  66.  
  67.  
  68. res = setning.executeQuery("select * from boktittel");
  69. while (res.next()) {
  70. String tittel2 = res.getString("tittel");
  71. System.out.println(tittel2);
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78. // if(isbnCheck == 0){
  79. // Executer setningene
  80. setning.executeQuery(insertIntoTittel);
  81. setning.executeQuery(insertIntoEksemplar);
  82. forbindelse.commit();
  83. return true;
  84. // } else {
  85. // return false;
  86. // }
  87. } catch (Exception e){
  88.  
  89. }finally{
  90.  
  91. lukkRes();
  92. lukkSetning();
  93. lukkForbindelse();
  94.  
  95. }
  96. return false;
  97. }
  98.  
  99. public static void main(String[] args){
  100. Database db = new Database();
  101. Bok nyBok = new Bok("500-500-CSE", "Min kamp", "Ole");
  102. db.regNyBok(nyBok);
  103.  
  104. }
  105. }
  106. // Connection forbindelse = DriverManager.getConnection(databasenavn);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement