Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package basicJDBCExample;
- import java.util.Scanner;
- import java.sql.*;
- public class Main {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- //loadDriver(); //Obsolete - only needed in rare cases.
- //try with resources (Java 7) - automatically calls connection.close() on end of try-catch block
- //Ensures that connections aren't left hanging
- try (Connection connection = DriverManager.getConnection("jdbc:mysql://ec2-52-30-211-3.eu-west-1.compute.amazonaws.com/s175124"
- , "s175124", "HMdWGDg3RRUx7sKh4rS3b")){
- Statement statement = connection.createStatement();
- Statement statement1 = connection.createStatement();
- System.out.println("Insert ID");
- int id = scan.nextInt();
- System.out.println("Insert indhold");
- Scanner scan1 = new Scanner(System.in);
- String indhold = scan1.nextLine();
- statement1.executeUpdate("INSERT INTO test VALUES ("+id+", \""+indhold+"\")");
- ResultSet resultSet = statement.executeQuery("SELECT * FROM test");
- System.out.println("Got resultset from database:");
- while (resultSet.next()){
- System.out.println(resultSet.getString(1) + ": " + resultSet.getString(2));
- }
- } catch (SQLException e) {
- //Remember to handle Exceptions gracefully! Connection might be Lost....
- e.printStackTrace();
- }
- }
- /**
- * Old School and should be obsolete - Use only if there is a complaint about the driver missing...
- */
- @Deprecated
- private static void loadDriver() {
- try {
- Class.forName("com.mysql.cj.Driver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement