Archive for January, 2007

Using JDBC with Java Applications and (Cox webspace) Applets 70

Saturday, January 13th, 2007

Using JDBC with Java Applications and Applets 70 connectToDB() method in our Hello object, you see that the connection from Java to the database is performed in a single line of code: connection = DriverManager.getConnection( “jdbc:mysql://localhost/accounts?user=&password=”); As you can see, the DriverManager is the catalyst used to create the connection to the database. This is consistent with its job of managing all JDBC drivers. When the getConnection() method is called, the DriverManager needs to decide what JDBC driver to use to connect to the database. Figure 5.1 shows how the DriverManager determines the proper JDBC driver to use with a given connection request. DriverManager Connector/J Oracle SQLServer MySQL Oracle Application SQLServer Figure 5.1 Determining the proper driver. Let s begin our discussion of obtaining a connection to the database by examining the API for the DriverManager. DriverManager API DriverManager is a static class that exposes methods for handling connections to a database as well as administrative methods for JDBC drivers. The following methods are those we might be interested in using: Connection getConnection(String URL) The DriverManager uses a reg istered driver in an attempt to build a connection to a specified database. Connection getConnection(String URL, Properties props) The DriverManager uses a registered driver in an attempt to build a connection to the specified database using the properties provided in the Properties object. Connection getConnection(String URL, String username, String password) The DriverManager uses a registered driver in an attempt to build a connection to the specified database using the provided username and password.
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra j2ee hosting services

Web host reseller - Hello World 69 Hello hello = new Hello();

Friday, January 12th, 2007

Hello World 69 Hello hello = new Hello(); hello.connectToDB(); hello.executeSQL(); } } Listing 5.1 Hello World. (continued) Since this is our first code for connecting Java to MySQL through Connector/J, we want to spend a fair amount of time going through it. First, note that this is a traditional Java application that instantiates an object and calls a few methods. When the Hello object is instantiated, the constructor is called to handle any initialization that needs to take place. Loading the Connector/J Driver In the constructor, we have placed code that attempts to locate and instantiate our Connector/J JDBC driver. The process begins with the Class.forName method. This method is designed to dynamically load a Java class at runtime. The Java Virtual Machine (JVM) uses the current system classpath (as well as any additional paths defined when the JVM was executed) to find the class passed to the method as a parameter. In our case, the system attempts to find the Driver class found in the com.mysql.jdbc package. In Chapter 4, we placed the Connector/J JAR file in the classpath of the JVM so it could be found. Once it finds the file, the code executes the newInstance() method to instantiate a new object from the Driver class. During the instantiation, the Driver will register itself with a static class called DriverManager, which is responsible for managing all JDBC drivers installed on the current system. If the JVM is unable to locate the driver, it outputs a message to the console and exits the application. Note that the DriverManager is designed to handle multiple JDBC driver objects just as long as they register with the class. This means that you can write a Java application that connects with more than one type of database system through JDBC. Note that simply loading the JDBC driver for a database doesn t result in any type of connection with the database. Using DriverManager to Connect to a Database Once our application object has been created and initialized, the code attempts to build a connection to the database. This is an important step, and therefore we ll spend some time discussing the connection code. If you look in the
Note: If you are looking for inexpensive but high quality provider to host and run your serlvet application check Astra servlet hosting services

Using JDBC with Java Applications and (Storefront hosting) Applets 68

Thursday, January 11th, 2007

Using JDBC with Java Applications and Applets 68 private void displaySQLErrors(SQLException e) { System.out.println(”SQLException: ” + e.getMessage()); System.out.println(”SQLState: ” + e.getSQLState()); System.out.println(”VendorError: ” + e.getErrorCode()); } public Hello() { try { Class.forName(”com.mysql.jdbc.Driver”).newInstance(); } catch (SQLException e) { System.err.println(”Unable to find and load driver”); System.exit(1); } } public void connectToDB() { try { connection = DriverManager.getConnection( “jdbc:mysql://localhost/accounts?user=&password=”); } catch(SQLException e) { displaySQLErrors(e); } } public void executeSQL() { try { Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery( “SELECT * FROM acc_acc”); while (rs.next()) { System.out.println(rs.getString(1)); } rs.close(); statement.close(); connection.close(); } catch(SQLException e) { displaySQLErrors(e); } } public static void main(String[] args) { Listing 5.1 Hello World. (continues)
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

CHAPTER 5 Using JDBC with Java Applications and

Wednesday, January 10th, 2007

CHAPTER 5 Using JDBC with Java Applications and Applets Now that we have a development environment put together, it s time to start writing Java code that will allow access to a MySQL database using the Connector/J JDBC driver. In the remaining chapters of this blog, it is our goal to exercise as much of the functionality found in the driver as possible. This chapter covers the basics of instantiating the driver, connecting to the database from Java, executing queries, and handling results. From a Java perspective, we look at doing all of these tasks from both applications and applets utilizing various GUI components to deal with the information transfer from the user to the database and from the database to the user. Hello World For the sake of tradition, the first application we build is Hello World. The code in Listing 5.1 creates a Java application and pulls information from a MySQL database. package mysql; import java.sql.*; public class Hello { Connection connection; Listing 5.1 Hello World. (continues) 67

Hint: If you are looking for very good and affordable webspace to host and run your tomcat hosting application check Sandzak.com tomcat web hosting provider

Installing MySQL, Java, and Connector/J 66 Testing the (Mysql webhost)

Tuesday, January 9th, 2007

Installing MySQL, Java, and Connector/J 66 Testing the Connector/J Installation Once you ve installed both Java and the Connector/J driver, create a test file called test.java and enter the following code into the file: public class test { public static void main(String[] args) { try { Class.forName(”com.mysql.jdbc.Driver”).newInstance(); System.out.println(”Good to go”); } catch (Exception E) { System.out.println(”JDBC Driver error”); } } } Save and exit the test file and compile it with this command: javac test.java Now execute the code with this command: java test If the Java Virtual Machine was able to find your Connector/J JAR file, you will see the text Good to go on the console; otherwise, you will see JDBC Driver Error . If you get an error, check that the JAR file is in the correct directory and/or check the CLASSPATH variable to be sure the full path to the JAR file has been included. Figure 4.3 shows all of these steps. Figure 4.3 Testing the Connector/J driver. What s Next Once you have installed all of the applications shown in this chapter, you are ready to start writing all sorts of Java applications that can access a MySQL database. In the next chapter, we begin looking at how to write applications and applets to access MySQL. We explore some of the basic functionality provided in the JDBC specification and implemented in Connector/J.
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

Storefront hosting - Installing Connector/J 65 Compile the code with the

Monday, January 8th, 2007

Installing Connector/J 65 Compile the code with the command javac hello.java If you get an error saying the javac command cannot be found, then you will need to check the path to the /bin directory; this means that the system is unable to find the Java compiler in the /bin directory. If things work out correctly, execute the Java with java Hello You should see the text Hello World It Works on your screen. If you don t see this text, check Sun s instructions to correct the installation. Installing Connector/J If you refer to Figure 4.1, you see that both the Production and Development areas have downloads available for Connector/J. Clicking on either of the links brings you to the respective page for that particular version of the code. In both cases, two files are available for download: a zip and a tar.gz. Most of the code in the remainder of this blog executes under the Production version of the code, but better performance and many small JDBC support changes are available in the Development 3.0 version. Our test machines used the 3.0 version of Connector/J. If you download the zip version of the code, we assume you are installing on a Windows box and that the tar/gz version for Linux or another Unix flavor. In either case, you need to uncompress the file to expose both the source code for the driver as well as a JAR file called (in 3.0) mysqlconnector-java-3.0.1-beta-bin.jar. This file contains all of the necessary class files for the driver. There are a few ways to install the driver. The first is to copy the /com and /org files into another directory listed in your classpath. Another option is to add the full path to the JAR file to your CLASSPATH variable. Finally, you can just copy the JAR file to the $JAVA_HOME/jre/lib/ext directory. On a Windows platform (if you installed SDK1.4.1), the directory is found at /program files/java/j2re1.4.1/lib/ext. Just copy the JAR file to that directory, and the library will be available for applications that execute within the Java Virtual Machine. On a Linux platform using SDK 1.4.1, the directory where you want to place the JAR file is /usr/java/j2sdk1.4.0/jre/lib/ext.
Note: If you are looking for reliable and quality webspace company to host and run your servlet application check professional servlet hosting services

Installing MySQL, Java, and Connector/J 64 Figure 4.2 (Non profit web hosting)

Sunday, January 7th, 2007

Installing MySQL, Java, and Connector/J 64 Figure 4.2 Testing MySQL. Installing Java Once the MySQL database server is installed, it s time to install Java. You can find the Java software development kit (SDK) at http://java.sun.com/ j2se/1.4.1/download.html. When you get to this page, you see downloads for numerous platforms and options for either the Java Runtime Environment (JRE) or SDK. Be sure to grab the SDK so you will be able to write code with Java. For Windows, you will find an EXE file to download. When the file has finished downloading, double-click on it to launch the installation wizard. Just a few clicks through the wizard is all it takes to install Java on Windows. When the Java installation wizard has finished installing Java, add the path to the /bin directory of the installation to the system PATH environment variable. That way, you will have access to the Java tools from a Windows command prompt. For Linux, you will find both an RPM and a self-extracting BIN file. If you download the RPM file, it will initially include a BIN extension, which you need to remove. Install the RPM with the rpm-I command. If you download the BIN self- extracting file, you need to change the file to have execution permissions with the chmod a+x command. Once permissions are set correctly, just execute the file to install Java. Full instructions for installing the Windows, Linux, and other environments can be found at http://java.sun.com/j2se/1.4.1/install.html if you run into problems. Testing the Java Installation Once Java has been installed, you should test the installation. To do this, create a file called hello.java and add the following code: public class hello { public static void main(String[] args) { System.out.println(”Hello World It Works”); } }

Hint: This post is supported by Gama web hosting php mysql provider

Comcast web hosting - Installing MySQL 63 Windows Installation The MySQL distribution

Sunday, January 7th, 2007

Installing MySQL 63 Windows Installation The MySQL distribution for Windows comes as a zip file and will need to be uncompressed before it can be used. Use WinZip or another tool of your choice to perform the decompression. Once you do, follow these instructions to install the server on a NT/2000/XP box: 1. Log on as the administrator user. 2. Stop the current MySQL if you re performing an upgrade. a. Open a command prompt. b. If MySQL is running as a service, type net stop where is the name of the MySQL server service name (normally the value is mysql). c. If MySQL is running as an application, change to the /bin directory of the MySQL installation and type mysqladmin u root shutdown. 3. If you are changing from the basic MySQL server to the Max version, you need to remove the service. 4. Locate the setup.exe file of the new installation from the uncompressed files. 5. After MySQL is installed, copy one of the configuration files in the installation directory to the root directory, c:/. If you are using the Max version of MySQL, configure the appropriate InnoDB or BDB options in the configuration file. 6. If you want to install the server as a service, type the command -mysqld-max-nt- –install (or –install-manual if you don t want Windows to automatically start the service when the machine boots). If you are installing MySQL on Windows 95, 98, or ME, the server cannot be used as a service and thus you will need to start and stop the server manually. Use the mysqladmin.exe application in the /bin directory to start MySQL. All Other Installations It is beyond the scope of this blog to provide installation instructions for every platform that MySQL supports. If you need to install MySQL on another platform, download the appropriate distribution and refer to http://www.mysql.com/documentation/mysql/bychapter/manual_Installing.html #Installing for complete instructions. Testing the MySQL Installation To determine that MySQL has been installed and is executing correctly, browse to the /bin directory of MySQL and execute the file mysql. You should see information like that shown in Figure 4.2.

Hint: If you are looking for very good and affordable webspace to host and run your tomcat hosting application check Sandzak.com tomcat web hosting provider

Installing MySQL, Java, and Connector/J 62 Figure 4.1 (1and1 web hosting)

Saturday, January 6th, 2007

Installing MySQL, Java, and Connector/J 62 Figure 4.1 The available MySQL downloads. Looking at the Production MySQL servers, you have two possibilities: MySQL and MySQL-Max. The MySQL download is a basic MySQL server without transaction support table types compiled into the binary. The MySQL-Max download includes support for the BDB table type in some platforms and the InnoDB table type in all platforms. Depending on which Production system you decided to download, you have the option of pulling the version for Windows, Linux, Solaris, and a host of other platforms. If you are downloading for Windows, you automatically get both the standard MySQL distribution as well as MySQL-Max. If you are downloading the Linux version, MySQL recommends using the RPM file for a clean installation. Note that you will have to download a number of Linux files. You should download the server and client programs files to have an operational development system. After you click on the correct platform version, the installation instructions change. Linux Installation For the Linux version of MySQL, you have two different files on your system. One has a name like MySQL-3.23/MySQL-3.23.52-1.i386.rpm, and the other has a name like MySQL-3.23/MySQL-client-3.23.52-1.i386.rpm. Since these are RPM files, they should install without much error on most recent Linux installations. The steps are 1. Install the server: type rpm -i MySQL-VERSION.i386.rpm. 2. Install the client tools: type rpm -i MySQL-client-VERSION.i386.rpm. The installation process places all of the code in /var/lib/mysql. In addition, the process makes entries in rc.d/ to automatically start MySQL when the machine boots.
Note: If you are looking for cheap and quality provider to host and run your java application check Astra java hosting services

CHAPTER 4 (Web hosting comparison) Installing MySQL, Java, and Connector/J If

Friday, January 5th, 2007

CHAPTER 4 Installing MySQL, Java, and Connector/J If you ve made it this far, you are ready to begin the process of integrating MySQL, Java, and Connector/J to build applications and sites that provide your users with a bounty of information. In this chapter, we explain how to install MySQL, Java, and Connector/J on your system. We cover both Linux and Windows, and for the most part, we show the basic installation that works on 99 percent of the environments out there. If these instructions don t work, you will need to turn to the product documentation. Installing MySQL You can find the MySQL database system at http://www.mysql.com under the downloads section of the site. Several different downloads are available, as shown in Figure 4.1. On the right-hand side of the MySQL Web page, you can see two major sections: Production and Development. The Production line of products has been thoroughly tested both within and outside MySQL. An organization can comfortably use the Production products in such an environment and be assured of stability and reliability. The Development line of products has been tested within MySQL with MySQL s own baseline tests but the products aren t at the level of production readiness. As you can see, the 4.0.x line of MySQL is currently in the Development stage and isn t recommended yet for production use. 61

Hint: This post is supported by Gama web hosting php services