Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.

Slides:



Advertisements
Similar presentations
16 Copyright © 2005, Oracle. All rights reserved. Using JDBC to Access the Database.
Advertisements

CE203 - Application Programming Autumn 2013CE203 Part 51 Part 5.
Murach’s Java SE 6, C21© 2007, Mike Murach & Associates, Inc.Slide 1.
Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
JDBC. Java Database Connectivity (JDBC) Use the java.sql package to query and update the database. JDBC is an API that allows java to communicate with.
1 JDBC Java Database Connectivity. 2 c.pdf
DT228/3 Web Development Databases. Database Almost all web application on the net access a database e.g. shopping sites, message boards, search engines.
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Three-Tier Architecture Oracle DB Server Apache Tomcat App Server Microsoft Internet Explorer HTML Tuples HTTP Requests JDBC Requests Java Server Pages.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
CS178 Database Management “JDBC”. What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java.
Jaeki Song JAVA Lecture 11 Java Database Connectivity.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Database Programming in Java Corresponds with Chapter 32, 33.
Views, Indexes and JDBC/JSP tutorial Professor: Dr. Shu-Ching Chen TA: Haiman Tian 1.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 32.1 Reviewing the Bookstore Application 32.2.
CS 160: Software Engineering October 1 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Index and JDBC/JSP tutorial Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
JSP program that interacts with HTML form & Access Data Base.
Chapter 8 Databases.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
DT228/3 Web Development Databases. Querying a database: Partial info Search engines, on-line catalogues often need to allow user to search a database.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 31.1 Reviewing the Bookstore Application 31.2.
Chapter 17 Accessing Databases with JDBC. JDBC JDBC provides a standard library for accessing relational databases. By using the JDBC API, you can access.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
JDBC CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
16 Java Database Connectivity. 2 Understand the JDBC Understand the steps of the JDBC: 1.) Importing packages 2.) Opening a connection to a database 3.)
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
CS562 Advanced Java and Internet Application Introduction to the Computer Warehouse Web Application. Java Server Pages (JSP) Technology. By Team Alpha.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
1 JDBC – Java Database Connectivity CS , Spring 2010.
1 Download current version of Tomcat from: g/tomcat/ g/tomcat/ Install it in: C:\Program Files\Apache.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
Database Processing with JSP ISYS 350. Database Applications Applications Database Server Queries/Updates Results.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.
ASP.NET Programming with C# and SQL Server First Edition
CS3220 Web and Internet Programming Database Access with JDBC
Note: To complete the examples in this section you need access to a database!! Most of the examples work for any database with JDBC drivers. However, connecting.
JDBC Database Management Database connectivity
CS320 Web and Internet Programming Database Access with JDBC
JDBC.
Objectives In this lesson, you will learn about:
Interacting with Database
Lecture 11 Database Connection
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use JavaScript, then you have your JSP.

A summary of the code for the JSP Although a JSP looks much like an HTML page, a JSP contains embedded Java code. To code a scriptlet that contains one or more Java statements, you use the tags. To display any expression that can be converted to a string, you use the tags. When you code a JSP, you can use the implicit request object. This object is named request. You can use the getParameter method of the request object to get the values of the parameters that are passed to the JSP.

The syntax for a JSP scriptlet The syntax for a JSP expression The syntax for getting a parameter from the implicit request object request.getParameter(parameterName);

Two scriptlets and an expression that display an HTML line 5 times <% int numOfTimes = 1; while (numOfTimes <= 5){ %> This line is shown of 5 times. <% numOfTimes++; } %>

How to code scriptlets and expressions Within a scriptlet, you can code one or more complete Java statements. Because these statements are Java statements, you must end each one with a semicolon. Within a JSP expression, you can code any Java expression that evaluates to a string. This includes Java expressions that evaluate to any of the primitive types, and it includes any object that has a toString method. Because a JSP expression is an expression, not a statement, you don’t end it with a semicolon.

A scriptlet that determines if a checkbox is checked <% String rockCheckBox = request.getParameter("Rock"); // returns the value "on" if checked, null otherwise. if (rockCheckBox != null){ %> You checked Rock music! <% } %>

A scriptlet that reads and displays multiple values from a list box <% String[] selectedCountries = request.getParameterValues("country"); // returns the values of items selected in list box. for (int i = 0; i < selectedCountries.length; i++){ %> <% } %>

Where and how to save a JSP JSPs are normally saved in the same directory as the HTML pages. This directory should be a subdirectory of the web applications directory for your server. If you’re running Tomcat on your PC, that directory is usually c:\tomcat\webapps or c:\jakarta- tomcat\webapps. If you’re using Tomcat on your local system, you can also use webapps\ROOT as the root directory for your applications. To make sure that the filename for a JSP is saved with the jsp extension when you’re using an HTML or text editor, you can enter the filename within quotes.

How to request a JSP When you use the Get method to request a JSP from an HTML form, the parameters are automatically appended to the URL. When you code or enter a URL that requests a JSP, you can add a parameter list to it starting with a question mark and with no intervening spaces. Then, each parameter consists of its name, an equals sign, and its value. To code multiple parameters, use ampersands (&) to separate the parameters.

A Form tag that requests a JSP Two URLs that request a JSP entry.jsp How to include parameters show_ _entry.jsp?firstName=John show_ _entry.jsp?firstName=John&lastName =Smith

Some JSP tags TagNamePurpose JSP scriptletTo insert a block of Java statements. JSP expressionTo display the string value of an expression. JSP directiveTo set conditions that apply to the entire JSP. JSP commentTo tell the JSP engine to ignore code.

A JSP comment <%-- Today’s date is. --%>

A JSP that displays the date and an instance variable

The code for the JSP Chapter 4 - List application <% String firstName = request.getParameter("firstName"); String lastName = request.getParameter("lastName"); String Address = request.getParameter(" Address"); User user = new User(firstName, lastName, Address); UserIO.addRecord(user, file);

The code for the JSP (continued) int localCount = 0; synchronized (this) { accessCount++; localCount = accessCount; } %> Today’s date is. This page has been accessed times.

How to use Java to work with a database

How to connect to a database Before you can get or modify the data in a database, you need to connect to it. To do that, you use the forName method of the Class class to load the driver. Then, you use the getConnection method of the DriverManager class to return a Connection object. When you use the forName method of the Class class, you must supply the driver name. This method throws a ClassNotFoundException. When you use the getConnection method of the DriverManager class, you must supply a URL for the database, a username, and a password. This method throws a SQLException. Although the connection string for each driver is different, the documentation for the driver should explain how to write a connection string for that driver.

Database URL syntax jdbc:subprotocolName:databaseURL How to connect to a MySQL db named murach Connection connection = null; try{ Class.forName("org.gjt.mm.mysql.Driver"); String dbURL = "jdbc:mysql://localhost/murach"; String username = "root"; String password = ""; connection = DriverManager.getConnection( dbURL, username, password); } catch(ClassNotFoundException e){message = "Database driver not found.";} catch(SQLException e){ message = "Error loading database driver: "+ e.getMessage(); }

How to create a result set that contains 1 row and 1 column Statement statement = connection.createStatement(); ResultSet userIDResult = statement.executeQuery( "SELECT UserID FROM User " + "WHERE Address = How to create a result set that contains multiple columns and rows Statement statement = connection.createStatement(); ResultSet products = statement.executeQuery( "SELECT * FROM Product ");

ResultSet methods for forward-only, read- only result sets MethodDescription next()Moves the cursor to the next row in the result set. last()Moves the cursor to the last row in the result set. close()Releases the result set’s JDBC and database resources. getRow()Returns an int value that identifies the current row of the result set.

Description To return a result set to a class, you use the createStatement method of a Connection object to create a Statement object. Then, you use the executeQuery method of the Statement object to execute a SELECT statement that returns a ResultSet object. By default, the createStatement method creates a forward- only, read-only result set. This means that you can only move the cursor through it from the first record to the last and you can’t update it. When a result set is created, the cursor is positioned before the first row. Then, you can use the methods of the ResultSet object to move the cursor. The createStatement, executeQuery, and next methods throw an SQLException. As a result, any code that uses these methods needs to catch or throw this exception.

How to retrieve data from a result set The getXXX methods can be used to return all eight primitive types. For example, the getInt method returns the int type and the getLong method returns the long type. The getXXX methods can also be used to return strings, dates, and times. For example, the getString method returns any object of the String class, and the getDate, getTime, and getTimestamp methods return objects of the Date, Time, and Timestamp classes of the java.sql package.

How to use the executeUpdate method to add a record String query = "INSERT INTO Product (ProductCode, ProductDescription, ProductPrice) " + "VALUES (‘ " + product.getCode() + "‘, " + "‘ " + product.getDescription() + "‘, " + "‘ " + product.getPrice() + "‘ )"; Statement statement = connection.createStatement(); int rowCount = statement.executeUpdate(query);

How to use the executeUpdate method to update a record String query = "UPDATE Product SET " + "ProductCode = ‘" + product.getCode() + "‘, " + "ProductDescription = ‘" + product.getDescription() + "‘, " + "ProductPrice = ‘ " + product.getPrice() + "‘ " + "WHERE ProductCode = ‘ " + product.getCode() + "‘"; Statement statement = connection.createStatement(); int rowCount = statement.executeUpdate(query);

How to use the executeUpdate method to delete a record String query = "DELETE FROM Product " + "WHERE ProductCode = ‘ " + productCode + "‘ "; Statement statement = connection.createStatement(); int rowCount = statement.executeUpdate(query);

The SQL Gateway application executing an INSERT statement

The SQL Gateway application after executing a SELECT statement

The JSP code (sql_gateway.jsp) <% String sqlStatement = (String) session.getAttribute("sqlStatement"); if (sqlStatement == null) sqlStatement = ""; String message = (String) session.getAttribute("message"); if (message == null) message = ""; %> Chapter 11 - The SQL Gateway application The SQL Gateway Enter an SQL statement and click the Execute button. Then, information about the statement will appear at the bottom of this page.

The JSP code (continued) SQL statement: SQL result: