import java.sql.*; /** * Driver class that runs sample JDBC code on a MySQL database * * Get MySQL at: http://dev.mysql.com/downloads/mysql/5.0.html * Get the MySQL JDBC at: * http://dev.mysql.com/downloads/connector/j/3.1.html * * Be sure to include the MySQL JDBC .jar file as an external library * in this Eclipse project (right click on this project, select * "Properties." Go-to Java Buildpath, select the "Libraries" tab, * and click the "External JAR" button. * * @author Alex Loddengaard (lodbot@cs) * */ public class Main { /** * Performs sample operations on the "test" database * @param args command-line arguments (ignored) * @throws RuntimeException if anything goes wrong when * interfacing with MySQL */ public static void main(String[] args) { Connection conn = getConnection(); //insert a "Katie Holmes" row into the "actors" table modifyQuery(conn, "INSERT INTO actors " + "VALUES (2, 'Katie Holmes', 'F')"); //query for all actors ResultSet results = selectQuery(conn, "SELECT * " + "FROM actors"); try { //loop through each row returned by the query and //print the name and id while (results.next()) System.out.println(results.getInt("id") + " " + results.getString("name")); } catch (SQLException e) { throw new RuntimeException("Error getting results: " + e); } //delete the "Katie Holmes" row from the actors table modifyQuery(conn, "DELETE FROM actors " + "WHERE name = 'Katie Holmes'"); } /** * Connects to MySQL using the MySQL JDBC driver * and returns a connection to the database * @return a connection to the database * @throws RuntimeException if an error occurs connecting to the database */ private static Connection getConnection() { //MySQL connection parameters String userName = "test"; String password = "test"; //host: localhost and database: test //host can be localhost, google.com, 192.168.1.100, etc String url = "jdbc:mysql://localhost/test"; try { //create a new instance of a Driver implementation Class.forName("com.mysql.jdbc.Driver").newInstance(); //have the DriverManager use each loaded Driver implementation to connect //with the given url, userName, and password Connection conn = DriverManager.getConnection(url, userName, password); return conn; } catch (Exception e) { throw new RuntimeException("Error connecting to database: " + e); } } /** * Queries the database using SELECT queries only and returns the results of the query * @param conn The database connection * @param query The SQL query string - only a SELECT query * @return A ResultSet that contains the rows returned by the query * @throws RuntimeException if the SQL query string is incorrect */ private static ResultSet selectQuery(Connection conn, String query) { try { PreparedStatement st = conn.prepareStatement(query); ResultSet rs = st.executeQuery(); return rs; } catch (SQLException e) { throw new RuntimeException("Error querying: " + e.getMessage()); } } /** * Queries the database using anything othe than SELECT queries * and returns the results of the query * @param conn The database connection * @param query The SQL query string - any non-SELECT query * @throws RuntimeException if the SQL query string is incorrect */ private static void modifyQuery(Connection conn, String query) { try { PreparedStatement st = conn.prepareStatement(query); st.executeUpdate(); } catch (SQLException e) { throw new RuntimeException("Error querying: " + e.getMessage()); } } }
Stuart Reges
Last modified: Wed May 23 15:44:58 PDT 2007