SHARE
TWEET

Untitled

a guest Jun 19th, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //import org.h2.Driver;
  2. //import java.sql.*;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.Statement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. /**
  10.  * Praktikum 5 - (5)
  11.  */
  12. public class db2
  13. {
  14.     // Instanzvariablen
  15.     private int x;
  16.     public static void main(String[] args) {
  17.         Connection conn = null;
  18.        
  19.         try {
  20.             // Verbindungsaufbau zur Datenbank
  21.             conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
  22.            
  23.             // Tabelle erstellen und Daten einf├╝gen
  24.             Statement stmt = conn.createStatement();
  25.             stmt.execute( "DROP TABLE Users IF EXISTS" );
  26.             stmt.execute( "CREATE TABLE Users ( ID INT PRIMARY KEY, Name VARCHAR(255) )" );
  27.             stmt.execute( "INSERT INTO Users VALUES( 1, 'Test User' )" );
  28.             stmt.execute( "INSERT INTO Users VALUES( 2, 'Test User 2' )" );
  29.             stmt.execute( "INSERT INTO Users VALUES( 3, 'Test User 3' )" );
  30.            
  31.             // Daten abfragen
  32.             ResultSet rs = stmt.executeQuery( "SELECT Name from Users" );
  33.             while( rs.next() ) System.out.println( rs.getString( "Name" ) );
  34.         }
  35.         catch (SQLException e) { e.printStackTrace(); }
  36.         finally
  37.         {
  38.             if( conn != null )
  39.             {
  40.                 try { conn.close(); }
  41.                 catch (SQLException e) { e.printStackTrace(); }
  42.             }
  43.         }
  44.     }
  45. }
RAW Paste Data
Pastebin PRO Autumn Special!
Get 40% OFF on Pastebin PRO accounts!
Top