Update.java Source code

Java tutorial

Introduction

Here is the source code for Update.java

Source

/*
    
Database Programming with JDBC and Java, Second Edition
By George Reese
ISBN: 1-56592-616-1
    
Publisher: O'Reilly
    
*/

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Example 3.3
 */
public class Update {
    public static void main(String args[]) {
        Connection con = null;

        if (args.length != 2) {
            System.out.println("Syntax: <java UpdateApp [number] [string]>");
            return;
        }
        try {
            String driver = "com.imaginary.sql.msql.MsqlDriver";

            Class.forName(driver).newInstance();
            String url = "jdbc:msql://carthage.imaginary.com/ora";
            con = DriverManager.getConnection(url, "borg", "");
            Statement s = con.createStatement();
            String test_id = args[0];
            String test_val = args[1];
            int update_count = s.executeUpdate(
                    "INSERT INTO test (test_id, test_val) " + "VALUES(" + test_id + ", '" + test_val + "')");

            System.out.println(update_count + " rows inserted.");
            s.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}