because we only want to pipe all of the results into the OutputField TextArea. We cast all of the returned results into a String however, if the database contains binary data, we could get some weird output, or even cause the program to break. When I tested the applet, the data source that I queried contained numerical and strings only. In Chapter 7, I ll show you how to deal with different data types in the ANSI SQL-2 specification, upon which the data types for the JDBC are based. Listing 4.7 Executing a statement. public String Select(String QueryLine) { // This is the method we called above in Listing 4.5. // We return a String, and use a String parameter for the entered query. String Output=”"; int columns; int pos; try { // Several of the following methods can throw exceptions if there was a // problem with the query, or if the connection breaks, or if // we improperly try to retrieve results. Statement stmt = con.createStatement(); // First, we instantiate a Statement class that is required to execute // the query. The Connection class returns a Statement object in its // createStatement method, which links the opened connection to // the passed-back Statement object. This is how the stmt instance // is linked to the actual connection to the data source. ResultSet rs = stmt.executeQuery(QueryLine); // The ResultSet in turn is linked to the connection to the data source // via the Statement class. The Statement class contains the executeQuery // method, which returns a ResultSet class. This is analagous to a // pointer that can be used to retrieve the results from the JDBC // connection. columns=(rs.getMetaData()).getColumnCount(); // Here we use the getMetaData method in the result set to return a // Metadata object. The MetaData object contains a getColumnCount // method which we use to determine how many columns of data // are present in the result. We set this equal to an integer // variable. Listing 4.8 Getting the Result and MetaData Information. while(rs.next()) { // Now, we use the next method of the ResultSet instance rs to fetch // each row, one by one. There are more optimized ways of doing // this–namely using the inputStream feature of the JDBC driver. // I show you an example of this in Chapter 9. for( pos=1; pos<=columns; pos++) { // Now let's get each column in the row ( each cell ), one by one. Output+=rs.getObject(pos)+" "; // Here we've used the general method for getting a result. The // getObject method will attempt to caste the result in the form
Do you want something as professional as you are? Well, we are, but our plans are even better, please check Web Hosting SSH and look why we are the best.