Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. import java.sql.*;
  2. public class Datenbank {
  3.    
  4.    
  5.    
  6.     static Connection dbConn= null;
  7.    
  8.     private static boolean loadDriver(String driver){
  9.         //Treiber laden
  10.         try{
  11.             Class.forName(driver);
  12.             return true;
  13.         }
  14.         catch(ClassNotFoundException e)
  15.         {
  16.             System.out.println("Konnte den JDBC-Treiber nicht laden:"+"("+e.getMessage()+")");
  17.             return false;
  18.         }
  19.         }
  20.    
  21.     private static boolean connect(String URL, String login, String passwort){
  22.         //Verbindung herstellen
  23.         try{
  24.             dbConn= DriverManager.getConnection(URL, login, passwort);
  25.             return true;
  26.             }
  27.         catch(SQLException e){
  28.             System.out.println("Konnte die Verbindung zur Datenbank unter "+login+"nicht herstellen"+"("+e.getMessage()+")");
  29.             return false;
  30.         }
  31.     }
  32.        
  33.     private static boolean executeSQLStatement(String sqlStatement){
  34.         //Statements ausführen(Create, Insert, Delete, update)
  35.         Statement stmt;
  36.         try{
  37.             stmt=dbConn.createStatement();
  38.             stmt.executeUpdate(sqlStatement);
  39.             stmt.close();
  40.             return true;
  41.         }
  42.         catch(SQLException e){
  43.             e.printStackTrace();
  44.             return false;
  45.         }
  46.     }
  47.    
  48.     private boolean executeQuery(String query){
  49.         Statement stmt;
  50.         try{
  51.             stmt= this.dbConn.createStatement();
  52.             ResultSet rs = stmt.executeQuery(query);
  53.             ausgabeResult(rs);
  54.             return true;
  55.         }
  56.         catch(SQLException e){
  57.             e.printStackTrace();
  58.             return false;
  59.         }
  60.        
  61.     }
  62.  
  63.     private void ausgabeResult(ResultSet rs) {
  64.         // TODO Auto-generated method stub
  65.         try{
  66.             int spalte=rs.getMetaData().getColumnCount();
  67.             while(rs.next()){
  68.                 String reihe= new String(" | ");
  69.                 for(int i=1; i<=spalte;i++){
  70.                     reihe=reihe.concat(rs.getString(i)+" | ");
  71.                     }
  72.                 System.out.println(reihe);
  73.             }
  74.         }
  75.             catch(SQLException e){
  76.                 e.printStackTrace();
  77.             }
  78.         }
  79.     private static boolean closeConnection(){
  80.         try{
  81.         dbConn.close();
  82.         dbConn=null;
  83.         return true;
  84.         }
  85.         catch(SQLException e){
  86.             System.out.println("Fehler beim Schließen der Datebankverbindung."+"("+e.getMessage()+")");
  87.             return false;
  88.         }
  89.     }
  90.     public static void main(String[] args){
  91.         String DBdriver= "oracle.jdbc.driver.OracleDriver";
  92.         String dbURL= "jdbc:oracle:thin:@gemini.informatik.uni-augsburg.de:1521:db";
  93.         String dbLogin ="1118590";
  94.         String dbPasswort= "1118598";
  95.         String query1= "CREATE TABLE inventory(partno INTEGER PRIMARY KEY, name CHAR(100), qonhand INTEGER)";
  96.         String query2= "INSERT INTO INVENTORY VALUES (300, 'Screw', 850)";
  97.        
  98.         loadDriver(DBdriver);
  99.         connect(dbURL,dbLogin,dbPasswort);
  100.         executeSQLStatement(query1);
  101.         executeSQLStatement(query2);
  102.         closeConnection();
  103.        
  104.     }
  105.        
  106.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement