Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class Sql {
- Connection conn;
- public Sql() {
- }
- protected void logIn(String a, String b){
- try{
- DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
- conn = DriverManager.getConnection("jdbc:oracle:thin:@//oracle-srv.edvsz.hs-osnabrueck.de:1521/oraclestud", a, b);
- System.out.println("LogIn successful.");
- }catch(SQLException e){
- System.out.println(e.getMessage());
- }
- }
- public void taskOneA() {
- try{
- Statement stmt = conn.createStatement();
- stmt.executeUpdate("DROP TABLE Kunde");
- System.out.println("Table ''Kunde'' dropped!");
- stmt.executeUpdate("CREATE TABLE Kunde (KNr INT NOT NULL , Name VARCHAR(25))");
- System.out.println("Table ''Kunde'' created!");
- stmt.executeUpdate("INSERT INTO Kunde(KNr, Name) VALUES(1,'Testmeyer')");
- stmt.executeUpdate("INSERT INTO Kunde(KNr, Name) VALUES(2,'Testmoehler')");
- stmt.executeUpdate("INSERT INTO Kunde(KNr, Name) VALUES(3,'Testrichter')");
- stmt.executeUpdate("INSERT INTO Kunde(KNr, Name) VALUES(4,'Testweide')");
- stmt.executeUpdate("INSERT INTO Kunde(KNr, Name) VALUES(5,'Testbaum')");
- System.out.println("Values in ''Kunde'' inserted!");
- }catch(SQLException e){
- System.out.println(e.getMessage());
- }
- }
- public void taskOneB() {
- String eingabe = "net end", tmp = "";
- boolean repeat = true;
- while(repeat == true) {
- eingabe = IO.readString("SQL Operation without semicolon and always uppercase. Type 'end' to close the program.\n");
- if(eingabe.contains(" ")){
- tmp = eingabe.substring(0, eingabe.indexOf(" "));
- }
- switch (eingabe) {
- case "end":
- repeat = false;
- break;
- default:
- switch(tmp) {
- case "SELECT":
- try{
- Statement stmt = conn.createStatement();
- ResultSet output = stmt.executeQuery(eingabe);
- ResultSetMetaData outputi = output.getMetaData();
- for(int i = 1; i < outputi.getColumnCount()+1; i ++) {
- System.out.print(outputi.getColumnName(i) + "\t|");
- }
- System.out.println();
- while(output.next()) {
- for(int i = 1; i < outputi.getColumnCount()+1; i ++) {
- System.out.print(output.getString(i)+ "\t|");
- }
- System.out.println();
- }
- System.out.println("");
- }catch(SQLException e) {
- System.out.println(e.getMessage());
- }
- break;
- case "DROP":
- try{
- Statement stmt = conn.createStatement();
- stmt.execute(eingabe);
- System.out.println("Table deleted!");
- }catch(SQLException e) {
- System.out.println(e.getMessage());
- }
- break;
- case "CREATE":
- try{
- Statement stmt = conn.createStatement();
- stmt.execute(eingabe);
- System.out.println("Table Created!");
- }catch(SQLException e) {
- System.out.println(e.getMessage());
- }
- break;
- case "UPDATE":
- try{
- Statement stmt = conn.createStatement();
- stmt.execute(eingabe);
- System.out.println("Values Updated!");
- }catch(SQLException e) {
- System.out.println(e.getMessage());
- }
- break;
- case "INSERT":
- try{
- Statement stmt = conn.createStatement();
- stmt.execute(eingabe);
- System.out.println("Inserted Values!");
- }catch(SQLException e) {
- System.out.println(e.getMessage());
- }
- break;
- case "DELETE":
- try{
- Statement stmt = conn.createStatement();
- stmt.execute(eingabe);
- System.out.println("Row Deleted!");
- }catch(SQLException e) {
- System.out.println(e.getMessage());
- }
- break;
- default:
- System.out.println("Unknown Command! Only:\nCREATE\nUPDATE\nDROP\nSELECT\nINSERT\nDELETE\n");
- break;
- }
- break;
- }
- }
- }
- public void taskOneC() {
- char a = IO.readChar("Interested in using a PreparedStatement to look for the KNr of a Name in Kunde?\nYes[y]\tNo[n]\n");
- if(a == 'y') {
- try{
- PreparedStatement pstmt = conn.prepareStatement("SELECT KNr, Name FROM Kunde WHERE Name = ?");
- pstmt.setString(1, IO.readString("Type Name: "));
- ResultSet output = pstmt.executeQuery();
- ResultSetMetaData outputi = output.getMetaData();
- for(int i = 1; i < outputi.getColumnCount()+1; i ++) {
- System.out.print(outputi.getColumnName(i) + "\t|");
- }
- System.out.println();
- while(output.next()) {
- for(int i = 1; i < outputi.getColumnCount()+1; i ++) {
- System.out.print(output.getString(i)+ "\t|");
- }
- System.out.println();
- }
- System.out.println("");
- }catch(SQLException e){
- System.out.println(e.getMessage());
- }
- }
- else {
- }
- }
- protected void logOut() {
- try{
- conn.close();
- System.out.println("LogOut successful.");
- }catch(SQLException e){
- System.out.println(e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement