Advertisement
Guest User

JDBC

a guest
May 26th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.83 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class Sql {
  4.    
  5.     Connection conn;
  6.    
  7.     public Sql()    {
  8.  
  9.     }
  10.    
  11.     protected void logIn(String a, String b){
  12.         try{
  13.             DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
  14.             conn = DriverManager.getConnection("jdbc:oracle:thin:@//oracle-srv.edvsz.hs-osnabrueck.de:1521/oraclestud", a, b);
  15.             System.out.println("LogIn successful.");
  16.         }catch(SQLException e){
  17.             System.out.println(e.getMessage());
  18.         }
  19.     }
  20.    
  21.     public void taskOneA()  {
  22.         try{
  23.             Statement stmt = conn.createStatement();
  24.             stmt.executeUpdate("DROP TABLE Kunde");
  25.             System.out.println("Table ''Kunde'' dropped!");
  26.             stmt.executeUpdate("CREATE TABLE Kunde (KNr INT NOT NULL , Name VARCHAR(25))");
  27.             System.out.println("Table ''Kunde'' created!");
  28.    
  29.             stmt.executeUpdate("INSERT INTO Kunde(KNr, Name) VALUES(1,'Testmeyer')");
  30.             stmt.executeUpdate("INSERT INTO Kunde(KNr, Name) VALUES(2,'Testmoehler')");
  31.             stmt.executeUpdate("INSERT INTO Kunde(KNr, Name) VALUES(3,'Testrichter')");
  32.             stmt.executeUpdate("INSERT INTO Kunde(KNr, Name) VALUES(4,'Testweide')");
  33.             stmt.executeUpdate("INSERT INTO Kunde(KNr, Name) VALUES(5,'Testbaum')");
  34.  
  35.             System.out.println("Values in ''Kunde'' inserted!");
  36.         }catch(SQLException e){
  37.             System.out.println(e.getMessage());
  38.         }
  39.     }
  40.    
  41.     public void taskOneB()  {
  42.         String eingabe = "net end", tmp = "";
  43.         boolean repeat = true;
  44.         while(repeat == true)   {
  45.             eingabe = IO.readString("SQL Operation without semicolon and always uppercase. Type 'end' to close the program.\n");
  46.                 if(eingabe.contains(" ")){
  47.                     tmp = eingabe.substring(0, eingabe.indexOf(" "));
  48.                 }
  49.             switch (eingabe)    {
  50.             case "end":
  51.                 repeat = false;
  52.                 break;
  53.             default:
  54.                
  55.                 switch(tmp) {
  56.                 case "SELECT":
  57.                     try{
  58.                         Statement stmt = conn.createStatement();
  59.                         ResultSet output = stmt.executeQuery(eingabe);
  60.                         ResultSetMetaData outputi = output.getMetaData();
  61.                         for(int i = 1; i < outputi.getColumnCount()+1; i ++)    {
  62.                             System.out.print(outputi.getColumnName(i) + "\t|");
  63.                         }
  64.                         System.out.println();
  65.                         while(output.next())    {
  66.                             for(int i = 1; i < outputi.getColumnCount()+1; i ++)    {
  67.                                 System.out.print(output.getString(i)+ "\t|");
  68.                             }
  69.                             System.out.println();
  70.                         }
  71.                         System.out.println("");
  72.                     }catch(SQLException e)  {
  73.                         System.out.println(e.getMessage());
  74.                     }
  75.                     break;
  76.                 case "DROP":
  77.                     try{
  78.                         Statement stmt = conn.createStatement();
  79.                         stmt.execute(eingabe);
  80.                         System.out.println("Table deleted!");
  81.                     }catch(SQLException e)  {
  82.                         System.out.println(e.getMessage());
  83.                     }
  84.                     break;
  85.                 case "CREATE":
  86.                     try{
  87.                         Statement stmt = conn.createStatement();
  88.                         stmt.execute(eingabe);
  89.                         System.out.println("Table Created!");
  90.                     }catch(SQLException e)  {
  91.                         System.out.println(e.getMessage());
  92.                     }
  93.                     break;
  94.                 case "UPDATE":
  95.                     try{
  96.                         Statement stmt = conn.createStatement();
  97.                         stmt.execute(eingabe);
  98.                         System.out.println("Values Updated!");
  99.                     }catch(SQLException e)  {
  100.                         System.out.println(e.getMessage());
  101.                     }
  102.                     break;
  103.                 case "INSERT":
  104.                     try{
  105.                         Statement stmt = conn.createStatement();
  106.                         stmt.execute(eingabe);
  107.                         System.out.println("Inserted Values!");
  108.                     }catch(SQLException e)  {
  109.                         System.out.println(e.getMessage());
  110.                     }
  111.                     break;
  112.                 case "DELETE":
  113.                     try{
  114.                         Statement stmt = conn.createStatement();
  115.                         stmt.execute(eingabe);
  116.                         System.out.println("Row Deleted!");
  117.                     }catch(SQLException e)  {
  118.                         System.out.println(e.getMessage());
  119.                     }
  120.                     break;
  121.                 default:
  122.                     System.out.println("Unknown Command! Only:\nCREATE\nUPDATE\nDROP\nSELECT\nINSERT\nDELETE\n");
  123.                     break;
  124.                 }
  125.                 break;
  126.             }
  127.        
  128.         }
  129.         }
  130.    
  131.     public void taskOneC()  {
  132.         char a = IO.readChar("Interested in using a PreparedStatement to look for the KNr of a Name in Kunde?\nYes[y]\tNo[n]\n");
  133.         if(a == 'y')    {
  134.             try{
  135.                 PreparedStatement pstmt = conn.prepareStatement("SELECT KNr, Name FROM Kunde WHERE Name = ?");
  136.                 pstmt.setString(1, IO.readString("Type Name: "));
  137.                 ResultSet output = pstmt.executeQuery();
  138.                 ResultSetMetaData outputi = output.getMetaData();
  139.                 for(int i = 1; i < outputi.getColumnCount()+1; i ++)    {
  140.                     System.out.print(outputi.getColumnName(i) + "\t|");
  141.                 }
  142.                 System.out.println();
  143.                 while(output.next())    {
  144.                     for(int i = 1; i < outputi.getColumnCount()+1; i ++)    {
  145.                         System.out.print(output.getString(i)+ "\t|");
  146.                     }
  147.                     System.out.println();
  148.                 }
  149.                 System.out.println("");
  150.             }catch(SQLException e){
  151.                 System.out.println(e.getMessage());
  152.             }
  153.         }
  154.         else    {
  155.            
  156.         }
  157.        
  158.     }
  159.    
  160.     protected void logOut() {
  161.         try{
  162.             conn.close();
  163.             System.out.println("LogOut successful.");
  164.         }catch(SQLException e){
  165.             System.out.println(e.getMessage());
  166.         }
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement