Archive for October, 2007

Wednesday, October 31st, 2007

} } class ColorGenerator { // This class is needed to produce colors that the pie chart can use to // color the slices properly. They are taken from the NetCharts color // class, NFColor. public ColorGenerator() { } int color_count = -1; // Keep a running count of the colors we have used. We’ll simply index // the colors in a String array, and call up the incremented counter to // get a new color. If you need more colors than are added below, you can // add more by pulling them from the NFColor class found in the NetCharts // package on the CD-ROM or Web site. String colors[] = {”aliceblue”,”antiquewhite”,”aqua”,”aquamarine”,”azure”,”beige”, “bisque”,”black”,”blanchedalmond”,”blue”,”blueviolet”,”brown”,”chocolate”, “cadetblue”,”chartreuse”,”cornsilk”,”crimson”,”cyan”}; public String next() { // Increment the color counter, and return a String which contains the // color at this index. color_count += 1; return colors[color_count]; } } // end example72.java Summary This chapter has shown you how to generate meaningful charts to represent data obtained from a query. We ve seen how to create both bar and pie charts. You can use the properties of the NetCharts package to customize your charts as you wish, and there are many more options in the package that haven t been shown in the examples here. In the next chapter, we will continue to discuss working with database query results, and we will provide a complete code example for showing SQL BLOB data types. It shows you how to get an image from the ResultSet, as well as how to add images or binary data to a table in a database. Chapter 8 The IconStore Multimedia JDBC Application
A musical ensemble is, by definition, a group of three or more musicians who gather to perform music. There are several denominations of ensembles according with their size and composition.Check band web hosting, and see why we are one of the best resources on the web for finding web hosting providers quickly and easily.

Tuesday, October 30th, 2007

} catch (Exception e) { System.out.println (e.getMessage()); } } // Below is the same as before except for the new ColorGenerator class // that we needed to produce distinct colors. public String[] getData( String QueryLine ) { int columns, pos; String column[]=new String[4]; boolean more; try { Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(QueryLine); columns=(rs.getMetaData()).getColumnCount(); column = new String[columns]; // Initialize the columns to be blank for(pos=1; pos<=columns; pos++) { column[pos-1]=""; } more=rs.next(); while(more) {for (pos=1; pos<=columns; pos++) { column[pos-1]+=(rs.getString(pos)); } more=rs.next(); for (pos=1; pos<=columns; pos++) { if(more) { column[pos-1]+=(","); } } } stmt.close(); // con.close(); } catch( Exception e ) { e.printStackTrace(); System.out.println(e.getMessage()); } return column; } public void destroy() { try {con.close();} catch( Exception e ) { e.printStackTrace(); System.out.println(e.getMessage()); }
Dear Canadians, choose Canadian Web Hosting and your Canadian website will benefit in every way from the reliability, security, and speed.

Monday, October 29th, 2007

ColorGenerator colorGen = new ColorGenerator(); // We need to assign colors to the pie slices automatically, so we use a // class that cycles through colors. See this class defined below. nData = new StringTokenizer(dataNumber, “,”); lData = new StringTokenizer(dataLabel, “,”); // We used our preformatted column data, and need to break it down to the // elements. We use the StringTokenizer to break the column string data // individual down by commas we inserted when we created the data. // We assume that dataNumber and dataLabel have the same number of // elements since we just generated them from the getData method. while(nData.hasMoreTokens()) { // Loop through the dataNumber and dataLabel and build the slice data: // ( 1234, darkBlue, “Label” ). This is what the pie chart class expects, // so we must parse our data and put it in this format. SliceData += “(”+nData.nextToken() + “, ” + colorGen.next() + “, ‘” + lData.nextToken() + “‘, green)”; System.out.println(SliceData); if (nData.hasMoreTokens()) {SliceData += “, “;} } try { // We already instantiated the pie chart // class(NFPieChartAPP) at the top of the applet. pie.init(); pie.start(); // Initialize and start the pie chart class. pie.loadParams( “Background=(black, RAISED, 4);”+ “Header=(’Cost Information (millions)’);”+ “LabelPos=0.7;”+ “DwellLabel = ('’, black, ‘TimesRoman’, 16);”+ “Legend = (’Legend’, black);”+ “LegendBox = (white, RAISED, 4);”+ “Slices=(12.3, blue, ‘Marketing’, cyan), (4.6, antiquewhite, ‘Sales’), (40.1, aqua, ‘Production’), (18.4, aquamarine, ‘Support’);”); // Above, we set the parameters for the pie chart, // including the data and labels which we generated // in the loop above ( SliceData ), and the Legend, // label position, header, and other properties. // Again, have a look at the NetCharts documentation // for all of the possible parameters. pie.loadParams (”Update”); // Tell the pie chart class we’ve sent it new // parameters to display.
We offer quality web hosting with only $3.99 per month with unlimited email addresses, unlimited bandwidth, and unlimited server space. Check our web hosting unlimited bandwidth section.

Sunday, October 28th, 2007

*/ import java.awt.*; import java.applet.Applet; import java.sql.*; import java.util.StringTokenizer; public class example72 extends java.applet.Applet { String url; String Name; Connection con; TextArea OutputField = new TextArea(10,35); NFPiechartApp pie; public void init() { setLayout(new BorderLayout()); url=”jdbc:msql://elanor/jdbctest”; pie = new NFPiechartApp(this); ConnectToDB(); add(”North”, OutputField); String columnData[] = getData(”select * from Cost”); ShowFormattedData(columnData); ShowChartData(columnData[1],columnData[0]); add(”Center”, pie); } public void ConnectToDB() { try { new imaginary.sql.iMsqlDriver(); con = DriverManager.getConnection(url, “prpatel”, “”); } catch( Exception e ) { e.printStackTrace(); System.out.println(e.getMessage()); } } public void ShowFormattedData(String[] columnD ) { int i; for ( i=0; i< columnD.length; i++) { OutputField.appendText(columnD[i]+"n"); } } public void ShowChartData(String dataNumber, String dataLabel) { StringTokenizer nData, lData; String SliceData = "";
CGI is used because it is far better and more physical than other ways , such as constructing miniatures for effects shots or hiring a cheap deal of extras for crowd scenes, and because it allows the creation of images that would not be feasible using any other technology.Check fore more details under our cgi web hosting section.

Saturday, October 27th, 2007

Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(QueryLine); columns=(rs.getMetaData()).getColumnCount(); column = new String[columns]; // Initialize the columns to be blank for(pos=1; pos<=columns; pos++) { column[pos-1]=""; } more=rs.next(); while(more) { for (pos=1; pos<=columns; pos++) { column[pos-1]+=(rs.getString(pos)); } more=rs.next(); for (pos=1; pos<=columns; pos++) { if(more) { column[pos-1]+=(","); } } } stmt.close(); } catch( Exception e ) { e.printStackTrace(); System.out.println(e.getMessage()); } return column; } That s it! We ve successfully queried a database, formatted the resulting data, and created a bar chart to present a visual representation of the results. Listing 7.5 shows the generation of a pie chart, and Figure 7.2 shows the pie chart applet. Figure 7.2 The pie chart applet. Listing 7.5 Dynamically generating a pie chart from a database query. /* Example 7-2: Pie chart
The European settlement at Melbourne was founded in 1835 by settlers coming from Tasmania (then known as Van Die men’s Land), where they had difficulty finding available land.We provides affordable, discount and cheapest Melbourne web hosting, free domain name, unlimited data transfer, unlimited emails, PHP, ASP, and database hosting,check Web Hosting Melbourne services.

Friday, October 26th, 2007

e.printStackTrace(); System.out.println(e.getMessage()); } } public void ShowChartData(String Data1, String Data2) { try { bar = new NFBarchartApp(this); // Instantiate the bar chart class bar.init(); bar.start(); // Initialize it, and start it running. // Below is where we load the parameters for the chart. // See the documentation at the NetCharts Web site, or // the CD-ROM for details. bar.loadParams( “Header = (’Salary Information’);”+ “DataSets = (’Salary’, red);”+ “DataSet1 = “+ Data1 + “;”+ “BarLabels = “+ Data2 + “;”+ “GraphLayout= HORIZONTAL;”+ “BottomAxis = (black, ‘TimesRoman’, 14, 0, 0,100000)” ); bar.loadParams (”Update”); // Tell the bar chart class we’ve put // some new parameters in. } catch (Exception e) { System.out.println (e.getMessage()); } } // More to come following some comments The bar chart class from the NetCharts package uses a method to load the values for the chart. We have to define the labels and corresponding values, but this is generally straightforward. Because our data is formatted in a comma-delimited fashion, we don t have to parse the data again to prepare it for use. In the next example (the pie chart example), we do have to parse it to put it in the proper format for the charting class to recognize it. Listing 7.4 picks up the code where we left off in Listing 7.3. Listing 7.4 Dynamically generating a bar chart from a database query Part II. public String[] getData( String QueryLine ) { int columns, pos; String column[]=new String[4]; boolean more; try {
Please check our cheap domain web hosting services, our secret is fast and stable servers, dedicated 24/7 customer support, and many useful FREE bonuses.

Thursday, October 25th, 2007

Example 7-1 */ import java.awt.*; import java.applet.Applet; import java.sql.*; public class example71 extends java.applet.Applet { String url; String Name; Connection con; TextArea OutputField = new TextArea(10,35); NFBarchartApp bar; // This is the bar chart class from the NetCharts package public void init() { setLayout(new BorderLayout()); url=”jdbc:msql://elanor/jdbctest”; // The URL for the database we wish to connect to ConnectToDB(); // Connect to the database. add(”North”, OutputField); // Add the TextArea for showing the data to the user String columnData[] = getData(”select * from Employee”); // Run a query that goes and gets the complete table listing; we can put // any query here and would optimally want to get only the columns we // need. ShowFormattedData(columnData); // Show the data in the TextArea ShowChartData(columnData[3],columnData[2]); // Now, pass the two data sets and create a bar chart add(”Center”, bar); // And add the bar chart to the applet’s panel } public void ShowFormattedData(String[] columnD ) { int i; for ( i=0; i< columnD.length; i++) { OutputField.appendText(columnD[i]+"n"); } } public void ConnectToDB() { try { new imaginary.sql.iMsqlDriver(); con = DriverManager.getConnection(url, "prpatel", ""); } catch( Exception e ) {
Don’t want to have just any web hosting, but web hosting provider who will share the same beliefs? You have found them. Our Church Web Hosting company will treat in you in appropriate way, the one you are accustomed to.

Wednesday, October 24th, 2007

} } } stmt.close(); // All done. Close the statement object. } catch( Exception e ) { e.printStackTrace(); System.out.println(e.getMessage()); } return column; // Finally, return the entire column[] array. } Showing The Results Now that we have the data nicely packaged into our Java object, how do we show it? The code in Listing 7.2 dumps the data in the object to the screen. We simply loop through the array and print the data. Listing 7.2 Code to print retrieved data to the console. public void ShowFormattedData(String[] columnD ) { int i; for ( i=0; i< columnD.length; i++) { System.out.println(columnD[i]+"n"); } } Charting Your Data Now that we ve covered the preliminaries, we are ready to get to the fun stuff! Instead of creating a package that has graphics utilities, we re going to use the NetCharts library, which is stored on the accompanying CD-ROM. The package on the CD is only an evaluation copy. Stop by http://www.netcharts.com to pick up the latest version (and some helpful documentation). We ll use the table in Table 7.1 and a bar chart to display the salary information for our fictional company. Figure 7.1 shows the applet that is generated from the code in Listing 7.3. Remember, the code for this example can be found on the accompanying CD-ROM, or at The Coriolis Group Web site at http://www.coriolis.com/jdbc-book. Figure 7.1 The bar chart applet. Listing 7.3 Dynamically generating a bar chart from a database query Part I. /*
We know it is important decision to make, effects your wallet, and the quality of your web site, so relax and visit shared web hosting.

Tuesday, October 23rd, 2007

int columns, pos; String column[]=new String[4]; // We have to initialize the column String variable even though we re- // declare it below. The reason is because the declaration below is in a // try{} statement, and the compiler will complain that the variable may // not be initialized. boolean more; try { Statement stmt = con.createStatement(); // Create a Statement object from the // Connection.createStatement method. ResultSet rs = stmt.executeQuery(QueryLine); // Execute the passed in query, and get // the ResultSet for the query. columns=(rs.getMetaData()).getColumnCount(); // Get the number of columns in the resulting table so we can // declare the column String array, and so we can loop // through the results and retrieve them. column = new String[columns]; // Create the column variable to be the exact number of // columns that are in the result table. // Initialize the column array to be blank since we’ll be adding // directly to them later. for(pos=1; pos<=columns; pos++) { column[pos-1]=""; } more=rs.next(); // Get the first row of the ResultSet. Loop through the ResultSet // and get the data, row-by-row, column-by-column. while(more) { for (pos=1; pos<=columns; pos++) { column[pos-1]+=(rs.getString(pos)); // Add each column to the respective column[] String array. } more=rs.next(); // Get the next row of the result if it exists. // Now add a comma to each array element to delimit this row is // done. for (pos=1; pos<=columns; pos++) { if(more) { // We only want to do this if this isn't the last row of the // table! column[pos-1]+=(",");
Here java web hosting you will find professional-grade java web hosting at affordable prices.

Monday, October 22nd, 2007

A Basic Java Object For Storing Results Although the JDBC provides you with the ResultSet class to get the data from an SQL query, you will still need to store and format within your program the results for display. The smart way to do this is in a re-usable fashion (by implementing a generic object or class) which allows you to re-use the same class you develop to retrieve data from a query in any of your JDBC programs. The code snippet in Listing 7.1 is a method that will keep your results in a Java object until you are ready to parse and display it. Let s begin by defining the data we will be getting from the source, and determining how we want to structure it within our Java applet. Remember that the ResultSet allows us to retrieve data in a row-by-row, column-by-column fashion; it simply gives us sequential access to the resulting table data. Table 7.1 shows the example table we will be using in this chapter. Table 7.1 Example table. emp_no first_name last_name salary 01234 Pratik Patel 8000 1235 Karl Moss 23000 0002 Keith Weiskamp 90000 0045 Ron Pronk 59999 53000 The optimal way to store this data in our Java program is to put each column s data in its own structure and then link the different columns by using an index; this will allow us to keep the columnar relationship of the table intact. We will put each column s data in an array. To simplify matters, we ll use the getString method, which translates the different data types returned by a query into a String type. Then, we ll take the data in a column and delimit the instances with commas. We ll use an array of String to do this; each place in the array will represent a different column. The data object we will create is shown here: table_data[0] => 01234,1235,0002,0045,0067 table_data[1] => Pratik,Karl,Keith,Ron,David table_data[2] => Patel,Moss,Weiskamp,Pronk,Friedel table_data[3] => 8000,23000,90000,59999,53000 Listing 7.1 shows the method we ll use to query the database and return a String array that contains the resulting table data. Listing 7.1 The getData method. public String[] getData( String QueryLine ) { // Run the QueryLine SQL query, and return the resulting columns in an // array of String. The first column is at index [0], the second at [1], // etc.
Please check java servlet web hosting services, here you will find professional-grade java servlet web hosting with the best prices.