Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.Queue;
- public class Database {
- private String brukernavn = "aadneny";
- private String passord = "yO20REny";
- private String databasedriver = "com.mysql.jdbc.Driver";
- private String databasenavn = "jdbc:mysql://mysql.stud.iie.ntnu.no:3306/" + brukernavn + "?user=" + brukernavn + "&password=" + passord;
- private Connection forbindelse;
- private Statement setning;
- private ResultSet res;
- public Database(){
- try {
- forbindelse = DriverManager.getConnection(databasenavn);
- } catch(Exception e){
- }
- }
- public void lukkForbindelse(){
- try{
- forbindelse.close();
- } catch (Exception e){
- }
- }
- public void lukkSetning(){
- try{
- setning.close();
- } catch(Exception e){
- }
- }
- public void lukkRes(){
- try{
- res.close();
- } catch(Exception e){
- }
- }
- public boolean regNyBok(Bok nyBok){
- try{
- forbindelse.setAutoCommit(false);
- String isbn = nyBok.getIsbn();
- String tittel = nyBok.getTittel();
- String forfatter = nyBok.getForfatter();
- String insertIntoTittel = "insert into boktittel(isbn, forfatter, tittel) values(" + isbn + "," + forfatter +"," + tittel + ")";
- String insertIntoEksemplar = "insert into eksemplar(isbn, eks_nr) values (" + isbn + ", 1)";
- // oppretter setningen
- setning = forbindelse.createStatement();
- // sjekker om ISB finnes
- res = setning.executeQuery("SELECT COUNT(*) FROM `boktittel` WHERE isbn LIKE '" + isbn +"'");
- int isbnCheck = -1;
- while(res.next()){
- isbnCheck = res.getInt(isbn);
- }
- res = setning.executeQuery("select * from boktittel");
- while (res.next()) {
- String tittel2 = res.getString("tittel");
- System.out.println(tittel2);
- }
- // if(isbnCheck == 0){
- // Executer setningene
- setning.executeQuery(insertIntoTittel);
- setning.executeQuery(insertIntoEksemplar);
- forbindelse.commit();
- return true;
- // } else {
- // return false;
- // }
- } catch (Exception e){
- }finally{
- lukkRes();
- lukkSetning();
- lukkForbindelse();
- }
- return false;
- }
- public static void main(String[] args){
- Database db = new Database();
- Bok nyBok = new Bok("500-500-CSE", "Min kamp", "Ole");
- db.regNyBok(nyBok);
- }
- }
- // Connection forbindelse = DriverManager.getConnection(databasenavn);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement