Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. import java.sql.*;
  2. import java.sql.Connection;
  3. import java.sql.Statement;
  4.  
  5. public class DatabaseConnectionClass {
  6.  
  7. public static void main (String[] args) {
  8. new DatabaseConnectionClass(args);
  9. }
  10. public DatabaseConnectionClass(String[] args) {
  11. try {
  12. Class.forName("com.mysql.jdbc.Driver").newInstance();
  13. String url = "jdbc:mysql://132.199.139.24:3306/mmdb17_robertbosek?user=r.bosek&password=mmdb";
  14. Connection Verbindung = DriverManager.getConnection(url);
  15. Statement SQLStatement = Verbindung.createStatement();
  16. if (args.length == 3) {
  17. String SQLString = new String("INSERT INTO Fahrer VALUES " + "(" + args[0] + ",'" + args[1] + "'," + args[2] + ");");
  18. SQLStatement.executeUpdate(SQLString);
  19. }
  20. String SQLString = new String("SELECT * FROM drivers");
  21. ResultSet Ergebnis = SQLStatement.executeQuery(SQLString);
  22. while(Ergebnis.next()) {
  23. System.out.println(Ergebnis.getString("prename") + " " + Ergebnis.getString("id_driver"));
  24. }
  25. Verbindung.close();
  26. }
  27. catch (ClassNotFoundException e) {
  28. System.err.print("Klasse nicht gefunden.");
  29. }
  30. catch (SQLException e) {
  31. System.err.print("SQL-Ausnahme: ");
  32. System.err.println(e.getMessage());
  33. }
  34. catch (Exception e) {
  35. System.err.print("Ein-/Ausgabefehler");
  36. }
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement