Lecture Notes


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Bring in laptops
  • Lecture Notes

    1. 1. MC365 JDBC in Servlets
    2. 2. Today We Will Cover: <ul><li>DBVisualizer </li></ul><ul><li>Using JDBC in servlets </li></ul><ul><li>Using properties files </li></ul>
    3. 3. DBVisualizer <ul><li>DBVisualizer is a tool used to manipulate a database. </li></ul><ul><ul><li>Other similar tools are SQL Plus, PL/SQL, Toad </li></ul></ul><ul><ul><li>DBA’s use these tools to perform database functions like setting up table spaces, tables, user access, triggers and stored procedures. </li></ul></ul><ul><ul><li>The focus of this class is not database administration (this is usually the job of the DBA), but it is important for software engineers to be familiar with some DBA functions. </li></ul></ul><ul><ul><ul><li>Especially in a 3-tier architecture </li></ul></ul></ul>
    4. 4. DBVisualizer <ul><li>For this class, a DBA has already set up an Oracle instance for us to use. </li></ul><ul><ul><li>The table spaces and user access have already been set up for us. </li></ul></ul><ul><ul><li>In most organizations a DBA will be responsible for these functions. </li></ul></ul><ul><li>As software engineers, we are going to use DBVisualizer to: </li></ul><ul><ul><li>Create and alter tables </li></ul></ul><ul><ul><li>Populate our tables </li></ul></ul><ul><ul><li>Test SQL statements </li></ul></ul><ul><ul><li>Write stored procedures (later) </li></ul></ul><ul><li>It is important for a software engineer to be comfortable with database functions and tools, even though the DBA will most likely be responsible for them. </li></ul>
    5. 5. Installing DBVisualizer <ul><li>Why use DBVisualizer? </li></ul><ul><ul><li>SQL Plus is command line. </li></ul></ul><ul><ul><li>DBVisualizer, PL/SQL and Toad are more GUI-oriented making them easier to use. </li></ul></ul><ul><ul><li>It’s Java-based and uses JDBC itself – perfect for testing in applications. </li></ul></ul><ul><ul><ul><li>If it works here, it will work in your servlet. </li></ul></ul></ul><ul><ul><li>It supports a variety of databases. </li></ul></ul><ul><ul><li>It is free. </li></ul></ul><ul><li>You can download DBVisualizer from here: http://www. minq .se/products/ dbvis / </li></ul>
    6. 6. Connecting to Our DB Using DBVisualizer <ul><li>Once you have installed DBVisualizer, you can connect to the Oracle instance set up for this class: </li></ul><ul><ul><li>Go to File – JDBC Driver Manager </li></ul></ul><ul><ul><ul><li>Point to the classes12.zip file </li></ul></ul></ul><ul><ul><li>Go to Database – Add Database Connection </li></ul></ul><ul><ul><li>Enter info: </li></ul></ul><ul><ul><ul><li>Database alias: goanna </li></ul></ul></ul><ul><ul><ul><li>JDBC driver: oracle.jdbc.driver.OracleDriver </li></ul></ul></ul><ul><ul><ul><li>URL: jdbc:oracle:thin:@goanna.bc.edu:1521:BCACAD1 </li></ul></ul></ul><ul><ul><ul><li>Userid: your username </li></ul></ul></ul><ul><ul><ul><li>Password: your password </li></ul></ul></ul><ul><ul><ul><ul><li>Try connecting using this password and make sure it works. </li></ul></ul></ul></ul><ul><ul><ul><ul><li>If you have problems or want me to change the password, let me know. </li></ul></ul></ul></ul>
    7. 7. SQL in DBVisualizer <ul><li>Once you are connected, you can navigate to your tablespace by clicking on the Database Objects tab. </li></ul><ul><li>You can also create and execute SQL statements in your tablespace by clicking on the SQL Commander tab. </li></ul><ul><li>Here are some example SQL statements you can use as models for creating, altering, updating and querying your tables. http://www2. bc . edu /~ bernier /MC365/Lecture Notes/ SQLExamples .doc </li></ul>
    8. 8. Using JDBC in Servlets <ul><li>To see an example of a simple Java application connecting to an Oracle instance via JDBC go to: http://www2. bc . edu /~ bernier /MC365/Lecture Notes/ JDBCTest .java </li></ul><ul><li>Note: Do not put a semicolon at the end of SQL statements in Java. </li></ul><ul><li>To see an example of a servlet connecting to an Oracle instance via JDBC go to: http://www2. bc . edu /~ bernier /MC365/Lecture Notes/ JDBCServlet .java Note: To use the Oracle driver (instead of the ODBC driver) to connect to an Oracle instance, you need to put the appropriate class files into the application server. These class files are in a zip file called Classes12.zip. You need to unzip the class files in this zip file into the following directory: c:Program FilesApache GroupTomcat 4.1commonclasses. Classes12.zip can be downloaded from Oracle’s site or here . </li></ul>
    9. 9. Properties Files <ul><li>What is a properties file? </li></ul><ul><ul><li>A properties file is a simple text file. You can create and maintain a properties file with just about any text editor. You should always create a default properties file. The name of this file begins with the base name of your ResourceBundle and ends with the .properties suffix. </li></ul></ul><ul><li>Why use a properties file? </li></ul><ul><ul><li>Properties files allow you to keep hard-coded values that might change in a text file. Text files are much easier to modify than java source code. </li></ul></ul><ul><ul><li>Note: Any time you make a change to a properties file, you need to restart the Tomcat for the changes to take effect. </li></ul></ul><ul><li>How do you reference a properties file in Java? </li></ul><ul><ul><li>Use the ResourceBundle object. See the JDBCServlet.java code for an example of how to use a properties file </li></ul></ul><ul><ul><li>Click on this link to see an example of a properties file: http://www2.bc.edu/~bernier/MC365/Lecture Notes/db.properties . </li></ul></ul><ul><ul><li>You can put the properties file used by the servlet in the same folder that the class file is in under Tomcat (e.g. WEB-INF/classes) </li></ul></ul>