Struts web hosting - Hello World 75 to values appropriate for the

Hello World 75 to values appropriate for the connection. After all of the properties are set, the object is used in a call to create a connection to the database. Handling Errors When dealing with connections to external sources, you must know how to handle errors that might occur. Both the JDBC driver and MySQL provide numerous types of errors. As you will see throughout our example program, try/catch blocks are provided to capture SQLException exceptions that are thrown by the Connector/J driver. When a SQLException exception is thrown, a call is made to the displaySQLErrors() method defined as a private method within our object. That method is shown here: private void displaySQLErrors(SQLException e) { System.out.println(”SQLException: ” + e.getMessage()); System.out.println(”SQLState: ” + e.getSQLState()); System.out.println(”VendorError: ” + e.getErrorCode()); } Like Connector/J, JDBC drivers implement three different specification- defined pieces of error information. These are the exception itself, the SQL- State, and a vendor error code. Our method outputs the values of these three components if an error occurs when we re trying to accomplish some JDBC task. For example, if we define a host address for our MySQL database system that doesn t exist, the following is displayed on the console: Unable to connect to host 08S01 0 In a production system, we probably want to log the error to an error file and attempt to recover from the error. This might include attempting to connect to another database. Executing Queries Through Statement Objects At this point in our code, we have pulled the Connector/J JDBC driver into our application and created a connection to the database. The example code in Listing 5.1 makes a call to an object method called executeSQL(), where the work to pull results from the database occurs. Within this method, the code builds a SQL statement object, executes the SQL, and displays the results. Building a Statement Object The first step in getting data from the MySQL database is to build a Statement object. The Statement object is designed to be an intermediary between the database connection and the results found from executing some SQL. When a
Note: If you are looking for cheap and quality provider to host and run your java application check Astra java hosting services

Comments are closed.