Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //import org.h2.Driver;
- //import java.sql.*;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.Statement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- /**
- * Praktikum 5 - (5)
- */
- public class db2
- {
- // Instanzvariablen
- private int x;
- public static void main(String[] args) {
- Connection conn = null;
- try {
- // Verbindungsaufbau zur Datenbank
- conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
- // Tabelle erstellen und Daten einfügen
- Statement stmt = conn.createStatement();
- stmt.execute( "DROP TABLE Users IF EXISTS" );
- stmt.execute( "CREATE TABLE Users ( ID INT PRIMARY KEY, Name VARCHAR(255) )" );
- stmt.execute( "INSERT INTO Users VALUES( 1, 'Test User' )" );
- stmt.execute( "INSERT INTO Users VALUES( 2, 'Test User 2' )" );
- stmt.execute( "INSERT INTO Users VALUES( 3, 'Test User 3' )" );
- // Daten abfragen
- ResultSet rs = stmt.executeQuery( "SELECT Name from Users" );
- while( rs.next() ) System.out.println( rs.getString( "Name" ) );
- }
- catch (SQLException e) { e.printStackTrace(); }
- finally
- {
- if( conn != null )
- {
- try { conn.close(); }
- catch (SQLException e) { e.printStackTrace(); }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement