Guest User

Untitled

a guest
Jun 10th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. package Øving6;
  2.  
  3. /**
  4.  * SQL & JDBC
  5.  * med unntakshåndtering
  6.  * @author Amund 30.jan.2012
  7.  */
  8.  
  9. import java.sql.*;
  10.  
  11. public class Database{
  12.     private Connection con;
  13.    
  14.     public Database()throws Exception{
  15.         String dbdriver = "org.apache.derby.jdbc.ClientDriver";
  16.         Class.forName(dbdriver);
  17.         String databasenavn = "jdbc:derby://localhost:1527/ov5_vprg;user=vprg;password=vprg";
  18.         this.con = DriverManager.getConnection(databasenavn);
  19.     }
  20.    
  21.     public void closeConnection(){
  22.         try{
  23.             con.close();
  24.             System.out.println("Connection Closed."); //for test purposes
  25.         }catch(SQLException sqle){
  26.             Opprydder.skrivMelding(sqle, "closeConnection()");
  27.         }
  28.     }
  29.    
  30.     public boolean regNewBook(Bok newBook){
  31.         /*
  32.          * Registers 1 copy of a new book, if said book does not excist in db
  33.          */
  34.         try{
  35.             boolean isReg = false;
  36.             String sqlInput = "insert into boktittel(isbn, forfatter, tittel) values('"+newBook.getIsbn()+"', '"+newBook.getForfatter()+"', '"+newBook.getTittel()+"')";
  37.             Statement state = con.createStatement();
  38.             if(state.executeUpdate(sqlInput)!=0){
  39.                 String sqlInput2 = "insert into eksemplar(isbn, eks_nr) values('"+newBook.getIsbn()+"', 1)";
  40.                 state.executeUpdate(sqlInput2);
  41.                 isReg = true;
  42.             }
  43.             state.close();
  44.             return isReg;
  45.         }catch(SQLException sqle){
  46.             Opprydder.skrivMelding(sqle, "regNewBook()");
  47.             return false;
  48.         }
  49.     }
  50.    
  51.     public int regNewCopy(String isbn){
  52.         int nyttNr = 1; //in case of no copies
  53.         boolean regOK=false;
  54.         int tries = 0;
  55.         do{
  56.             ResultSet res = null;
  57.             Statement state = null;
  58.             try{
  59.                 String sqlInput = "select max(eks_nr) as maks from eksemplar where isbn = '"+isbn+"'";
  60.                 state = con.createStatement();
  61.                 res = state.executeQuery(sqlInput);
  62.                
  63.                 res.next();
  64.                 nyttNr = res.getInt("maks")+1;
  65.                
  66.                 sqlInput = "insert into eksemplar(isbn, eks_nr) values('"+isbn+"', "+nyttNr+")";
  67.                 if(state.executeUpdate(sqlInput)!=0){
  68.                     regOK=true;
  69.                 }
  70.             }catch(SQLException sqle){
  71.                 if(tries < 4){
  72.                     tries++;
  73.                 }else{
  74.                     Opprydder.skrivMelding(sqle, "regNewCopy()");
  75.                     return 0;
  76.                 }
  77.             }finally{
  78.                 Opprydder.lukkResSet(res);
  79.                 Opprydder.lukkSetning(state);
  80.             }
  81.         }while(!regOK);
  82.         return nyttNr;
  83.     }
  84.    
  85.     public boolean lendCopy(String isbn, String name, int copyNum){
  86.         boolean isOk=false;
  87.         Statement stm = null;
  88.         try{
  89.             String sqlInput = "update eksemplar set laant_av = '"+name+"' where isbn = '"+isbn+"' and eks_nr = "+copyNum;
  90.             stm = con.createStatement();
  91.            
  92.             if(stm.executeUpdate(sqlInput)!=0){
  93.                 isOk=true;
  94.             }
  95.             stm.close();
  96.             return isOk;
  97.         }catch(SQLException sqlerr){
  98.             Opprydder.skrivMelding(sqlerr, "lendCopy");
  99.             return isOk;
  100.         }
  101.     }
  102.    
  103.     public static void main(String[] args)throws Exception{
  104.         Database db = new Database();
  105.         String isbn = "1337";
  106.         String tittel = "Pedofile i dagens samfunn";
  107.         String forfatter = "Rune Aasvestad";
  108.         if(db.regNewBook(new Bok(isbn, tittel, forfatter))) System.out.println("Bok registrert");
  109.         System.out.println("Eksemplar registrert som eksemplarnr: "+db.regNewCopy("1337"));
  110.         if(db.lendCopy("1337","Rune Aasvestad",1)) System.out.println("eksemplar nr1 av bok med isbn 1337 utlånt til Rune Aasvestad");
  111.         db.closeConnection();
  112.     }
  113. }
Add Comment
Please, Sign In to add comment