Products Support Documentation Download
tutorials/HelloWorldJDBC/HelloWorld_JDBCTutorial_main.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class HelloWorld_JDBCTutorial_main {
public static void main(String[] args) throws SQLException {
// Connection object used for connecting to driver
Connection Conn = null;
// Statement object used for executing SQL Statements
Statement Stmt = null;
// Prepared Statement object used for preparing SQL Statements
PreparedStatement prepStmt = null;
// Result Set object used for storing queried values
ResultSet rs = null;
// The try block contains the main portion of the code. The code is contained
// inside here so that if it fails the exception will be caught inside the
// catch blocks.
try
{
// Connects to driver using connection URL and passed into Connection
// Object
Conn = DriverManager.getConnection("jdbc:raima:rdm://local");
// Creates the Statement object to be used for future SQL Statement
// executions
Stmt = Conn.createStatement();
// Opens database
Stmt.executeUpdate("USE hello_worldJDBC");
//Used to clear table if there is previous data
Stmt.executeUpdate("DELETE FROM hello_table");
// Prepares the insert statement for execution
prepStmt = Conn.prepareStatement("INSERT INTO hello_table (f00) VALUES (?)");
// Performs a setter method for a string setting the value of the
// parameter above
prepStmt.setString(1, "Hello World!");
//Executes prepared Statement
prepStmt.execute();
// Commits all changes
Conn.commit();
// Executes a select statement and stores queried results in the result
// set
rs = Stmt.executeQuery("SELECT * FROM hello_table");
// Loops through the result set, checking for when the next value is
// null. We only expect 1 iteration. The contents of helloString are
// then received from the getter method. The results are then printed
// to the screen
while (rs.next() != false)
{
String helloString = rs.getString(1);
System.out.println(helloString);
}
}
// The catch block is used to catch an exceptions that are thrown above and
// display them to the screen. Currently the only type of exception we can
// expect would be a SQLException otherwise we would have to have another
// catch block for each possible exception
catch (SQLException exception)
{
System.err.println("An exception has occured : " + exception.toString());
System.exit (1);
}
// The finally block is used to execute after the try block has been
// performed. It is also executed if an exception is thrown, this way you
// can clean up any open objects without missing the .close() on a failure.
// Every object must be closed as you would a handle in ODBC.
finally
{
if (rs != null)
{
rs.close();
}
if (prepStmt != null)
{
prepStmt.close();
}
if (Stmt != null)
{
Stmt.close();
}
if (Conn != null)
{
Conn.close();
}
}
}
}