Advertisement
Guest User

Untitled

a guest
Oct 7th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package pkgData;
  7.  
  8.  
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.sql.Statement;
  15. import java.util.ArrayList;
  16. import java.util.Collection;
  17.  
  18. /**
  19.  *
  20.  * @author schueler
  21.  */
  22. public class Database {
  23.    
  24.     private static final String url = "jdbc:oracle:thin:d4b06/d4b@192.168.128.152:1521:test";
  25.     private String ip = null;
  26.     private static Database db = null;
  27.     private static final String USER = "schueler";
  28.     private static final String PASSWD = "schueler";
  29.     private Connection conn = null;
  30.    
  31.     private Database() {}
  32.    
  33.     public Database newInstance(String _ip) {
  34.         if (db == null) {
  35.             db = new Database();
  36.             ip = _ip;
  37.         }
  38.         return db;
  39.     }
  40.    
  41.     public void createConnection() throws SQLException {
  42.         if(conn == null) {
  43.             DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  44.         }
  45.        
  46.         conn = DriverManager.getConnection(url, USER, PASSWD);
  47.     }
  48.    
  49.     public Collection<Quiz> getQuizzes() throws SQLException {
  50.         ArrayList<Quiz> collQuiz = new ArrayList<>();
  51.         String select = "select quid, bezeichnung from quiz order by bezeichnung";
  52.        
  53.         createConnection();
  54.        
  55.         Statement stmt = (Statement) conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
  56.         ResultSet rs = stmt.executeQuery(select);
  57.        
  58.         while(rs.next()) {
  59.             Quiz quiz = new Quiz(rs.getInt("qid"), rs.getString("bezeichnung"));
  60.            
  61.             collQuiz.add(quiz);
  62.         }
  63.                  
  64.         conn.close();  
  65.         return collQuiz;
  66.     }
  67.    
  68.     public void addQuiz(Quiz quiz) throws SQLException {
  69.         String insert = "insert into quiz values(?,?)";
  70.        
  71.         createConnection();
  72.        
  73.         PreparedStatement stmt = conn.prepareStatement(insert);
  74.         stmt.setInt(1, quiz.getIdQuiz());
  75.         stmt.setString(2, quiz.getQuizName());
  76.         stmt.executeUpdate();
  77.         conn.close();
  78.     }
  79.    
  80.     public void updateQuiz(Quiz quiz) throws SQLException {
  81.         String update = "update quiz set bezeichnung = ?" + "where qid = ?";
  82.        
  83.         createConnection();
  84.        
  85.         PreparedStatement stmt = conn.prepareStatement(update);
  86.         stmt.setString(2, quiz.getQuizName());
  87.         stmt.executeUpdate();
  88.         conn.close();
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement