📄 newcustomerexample.java
字号:
import java.sql.*;public class NewCustomerExample{ // Private Instance variable for the Connection private Connection conn = null; // Default Constructor public NewCustomerExample( Connection connection ) { super(); conn = connection; } public Customer insertNewCustomer( Customer aCustomer ) throws SQLException { // Local variable for the Statement object Statement stmt = null; ResultSet rs = null; // Create a Statement object from the Connection stmt = getConnection().createStatement(); // Get the highest customer id and increment it by 1 for the next id String maxCustomerNbrSQL = "SELECT MAX(CUSTID) FROM CUSTOMER"; rs = stmt.executeQuery( maxCustomerNbrSQL ); rs.next(); int maxNbr = rs.getInt( 1 ); aCustomer.setId( String.valueOf( maxNbr + 1 ) ); // Ask the Customer for it's insert sql statement String sql = aCustomer.getInsertSQL(); stmt.executeUpdate( sql ); // return the customer instance with the customer number set return aCustomer; } // A method to update an existing customer in the database public void updateCustomer( Customer cust ) { // Local variable for the Statement object Statement stmt = null; ResultSet rs = null; try { String sql = cust.getUpdateSQL(); // Put a line here so that we can read the output better System.out.println(""); System.out.println( sql ); // Create a Statement object from the Connection stmt = getConnection().createStatement(); stmt.executeUpdate( sql ); } catch( SQLException ex ) { ex.printStackTrace(); } } public void printAllCustomers() { // Local variable for the Statement object Statement stmt = null; ResultSet rs = null; try { // Create a Statement object from the Connection stmt = getConnection().createStatement(); // Ask the Customer for it's insert sql statement String sql = Customer.getReadAllSQL(); rs = stmt.executeQuery( sql ); while( rs.next() ) { Customer cust = new Customer(); String id = rs.getString( 1 ); String name = rs.getString( 2 ); String city = rs.getString( 3 ); // Had to do this because the database type was a char and // there were too many spaces at the end for it to print out // and look reasonable. This is a bad thing, but neccessary for // example here in this book. if ( city != null ) city = city.substring( 0, 15 ); String state = rs.getString( 4 ); String zip = rs.getString( 5 ); cust.setId( id ); cust.setName( name ); cust.setCity( city ); cust.setState( state ); cust.setZip( zip ); System.out.println( cust.toString() ); } } catch( SQLException ex ) { ex.printStackTrace(); } } // Private accessor for the connection private Connection getConnection() { return conn; } public static void main(String[] args) { try { // Use the previous DatabaseManager to aquire a connection Connection conn = DatabaseManager.getConnection(); NewCustomerExample example = new NewCustomerExample( conn ); // Create a new Customer object to be inserted into the database // Notice we are not populating the customer nbr field because the // database is setup to do that for us Customer cust = new Customer(); cust.setName( "Josh Alan" ); cust.setCity( "Snellville" ); cust.setState( "GA" ); cust.setZip( "12345" ); // Call the insert method so that the customer will be inserted example.insertNewCustomer( cust ); // Print the customer records, including the one just inserted example.printAllCustomers(); // Let's update a field in the customer cust.setZip( "54321" ); example.updateCustomer( cust ); // Print the records out again. The zip for the one that was // just added should be 54321, not 12345 example.printAllCustomers(); // Always make sure to close the connection when you are finished conn.close(); } catch( SQLException ex ) { ex.printStackTrace(); } catch( Exception ex ) { ex.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -