// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class P3 {
    public static void main(String[] args) throws Exception {
        // open database
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "");
        // write message to database
        PreparedStatement ins = con.prepareStatement("INSERT INTO msgs(msg) VALUES(?)");
        ins.setString(1, "Hi from Java!");
        ins.executeUpdate();
        ins.close();
        // dump all messages in database
        Statement sel = con.createStatement();
        ResultSet rs = sel.executeQuery("SELECT msg FROM msgs");
        while(rs.next()) {
            System.out.println(rs.getString(1));
        }
        rs.close();
        sel.close();
        // clsoe database
        con.close();
    }
}
