SlideShare a Scribd company logo
Advanced Java Programming - Unit IV Roy Antony Arnold G Sr. Lecturer Einstein College of Engineering Tirunelveli, Tamilnadu, India
Expressions.jsp <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> <HTML> <HEAD> <TITLE>JSP Expressions</TITLE> <META NAME=&quot;author&quot; CONTENT=&quot;Marty Hall&quot;> <META NAME=&quot;keywords” CONTENT=&quot;JSP,expressions,JavaServer,Pages,servlets&quot;> <META NAME=&quot;description” CONTENT=&quot;A quick example of JSP expressions.&quot;> <LINK REL=STYLESHEET HREF=&quot;JSP-Styles.css” TYPE=&quot;text/css&quot;> </HEAD> <BODY> <H2>JSP Expressions</H2> <UL> <LI>Current time:  <%= new java.util.Date() %> <LI>Your hostname:  <%= request.getRemoteHost() %> <LI>Your session ID:  <%= session.getId() %> <LI>The <CODE>testParam</CODE> form parameter:  <%= request.getParameter(&quot;testParam&quot;) %> </UL> </BODY> </HTML>
Output
Example for Scriptlets <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> <HTML> <HEAD> <TITLE>Color Testing</TITLE> </HEAD> <% String bgColor = request.getParameter(&quot;bgColor&quot;); boolean hasExplicitColor; if (bgColor != null) { hasExplicitColor = true; } else { hasExplicitColor = false; bgColor = &quot;WHITE&quot;; } %> <BODY BGCOLOR=&quot; <%= bgColor %>&quot;> <H2 ALIGN=&quot;CENTER&quot;>Color Testing</H2> <% if (hasExplicitColor) { out.println(&quot;You supplied an explicit background color of &quot; + bgColor + &quot;.&quot;); } else { out.println(&quot;Using default background color of WHITE. &quot; + &quot;Supply the bgColor request attribute to try a standard color, an RRGGBB value, or to see &quot; + &quot;if your browser supports X11 color names.&quot;); } %> </BODY> </HTML>
JDBC There are seven standard steps in querying databases: 1. Load the JDBC driver. 2. Define the connection URL. 3. Establish the connection. 4. Create a statement object. 5. Execute a query or update. 6. Process the results. 7. Close the connection.
1. Load the JDBC Driver try { Class.forName(&quot;connect.microsoft.MicrosoftDriver&quot;); Class.forName(&quot;oracle.jdbc.driver.OracleDriver&quot;); Class.forName(&quot;com.sybase.jdbc.SybDriver&quot;); }  catch(ClassNotFoundException cnfe) { System.err.println(&quot;Error loading driver: &quot; + cnfe); }
2. Define the Connection URL String host = &quot;dbhost.yourcompany.com&quot;; String dbName = &quot;someName&quot;; int port = 1234; String oracleURL = &quot;jdbc:oracle:thin:@&quot; + host + &quot;:&quot; + port + &quot;:&quot; + dbName; String sybaseURL = &quot;jdbc:sybase:Tds:&quot; + host + &quot;:&quot; + port + &quot;:&quot; + &quot;?SERVICENAME=&quot; + dbName;
3. Establish the Connection String username = “sample&quot;; String password = &quot;secret&quot;; Connection connection = DriverManager.getConnection(oracleURL, username, password); DatabaseMetaData dbMetaData = connection.getMetaData(); String productName = dbMetaData.getDatabaseProductName(); System.out.println(&quot;Database: &quot; + productName); String productVersion = dbMetaData.getDatabaseProductVersion(); System.out.println(&quot;Version: &quot; + productVersion);
4. Create a Statement Statement statement = connection.createStatement(); 5. Execute a Query String query = &quot;SELECT col1, col2, col3 FROM sometable&quot;; ResultSet resultSet = statement.executeQuery(query);
6. Process the Results while(resultSet.next()) { System.out.println(results.getString(1) + &quot; &quot; + results.getString(2) + &quot; &quot; + results.getString(3)); } 7. Close the Connection connection.close();
Example 1 import java.sql.*; /** A JDBC example that connects to either an Oracle or a Sybase database and prints out the values of predetermined columns in the &quot;fruits&quot; table. */ public class FruitTest { public static void main(String[] args) { if (args.length < 5) { printUsage(); return; } String vendorName = args[4]; int vendor = DriverUtilities.getVendor(vendorName); if (vendor == DriverUtilities.UNKNOWN) { printUsage(); return; }
String driver = DriverUtilities.getDriver(vendor); String host = args[0]; String dbName = args[1]; String url = DriverUtilities.makeURL(host, dbName, vendor); String username = args[2]; String password = args[3]; showFruitTable(driver, url, username, password); } /** Get the table and print all the values. */ public static void showFruitTable(String driver, String url, String username, String password) {
try { // Load database driver if not already loaded. Class.forName(driver); // Establish network connection to database. Connection connection = DriverManager.getConnection(url, username, password); // Look up info about the database as a whole. DatabaseMetaData dbMetaData = connection.getMetaData(); String productName = dbMetaData.getDatabaseProductName(); System.out.println(&quot;Database: &quot; + productName); String productVersion = dbMetaData.getDatabaseProductVersion(); System.out.println(&quot;Version: &quot; + productVersion + &quot;\n&quot;); System.out.println(&quot;Comparing Apples and Oranges\n&quot; + &quot;================&quot;); Statement statement = connection.createStatement();
String query = &quot;SELECT * FROM fruits&quot;; // Send query to database and store results. ResultSet resultSet = statement.executeQuery(query); // Look up information about a particular table. ResultSetMetaData resultsMetaData = resultSet.getMetaData(); int columnCount = resultsMetaData.getColumnCount(); // Column index starts at 1 (a la SQL) not 0 (a la Java). for(int i=1; i<columnCount+1; i++) { System.out.print(resultsMetaData.getColumnName(i) +&quot; &quot;); } System.out.println(); // Print results. while(resultSet.next()) {
// Quarter System.out.print(&quot; &quot; + resultSet.getInt(1)); // Number of Apples System.out.print(&quot; &quot; + resultSet.getInt(2)); // Apple Sales System.out.print(&quot; $&quot; + resultSet.getFloat(3)); // Top Salesman System.out.println(&quot; &quot; + resultSet.getString(4)); } } catch(ClassNotFoundException cnfe) { System.err.println(&quot;Error loading driver: &quot; + cnfe); } catch(SQLException sqle) { System.err.println(&quot;Error connecting: &quot; + sqle); } } private static void printUsage() { System.out.println(&quot;Usage: FruitTest host dbName &quot; + &quot;username password oracle|sybase.&quot;); } } Java FruitTest db.ece fruitsale student xxxx oracle
Example 2 import java.sql.*; /** Connect to Oracle or Sybase and print &quot;employees&quot; table. */ public class EmployeeTest { public static void main(String[] args) { if (args.length < 5) { printUsage(); return; } String vendorName = args[4]; int vendor = DriverUtilities.getVendor(vendorName); if (vendor == DriverUtilities.UNKNOWN) { printUsage(); return; } String driver = DriverUtilities.getDriver(vendor); String host = args[0]; String dbName = args[1]; String url = DriverUtilities.makeURL(host, dbName, vendor); String username = args[2]; String password = args[3]; DatabaseUtilities.printTable(driver, url, username, password, &quot;employees&quot;, 12, true); } private static void printUsage() { System.out.println(&quot;Usage: EmployeeTest host dbName &quot; + &quot;username password oracle|sybase.&quot;); } }

More Related Content

DOCX
My java file
PPS
PHP Security
PDF
Building RESTful API
KEY
PHP security audits
KEY
Testing JS with Jasmine
PPT
PPTX
Laravel Unit Testing
PPT
My java file
PHP Security
Building RESTful API
PHP security audits
Testing JS with Jasmine
Laravel Unit Testing

What's hot (17)

PDF
OCP Java SE 8 Exam - Sample Questions - Java Streams API
PDF
Your code are my tests
ODP
Object Oriented Design Patterns for PHP
PDF
QA for PHP projects
PPTX
Javascript Testing with Jasmine 101
ODP
Bring the fun back to java
ODP
Django for Beginners
PPT
Advanced PHPUnit Testing
PPT
PHP Security
PDF
Intro to testing Javascript with jasmine
PDF
Spring Certification Questions
PPT
Phpunit testing
PPT
Django, What is it, Why is it cool?
ODP
An introduction to SQLAlchemy
PDF
PhpUnit Best Practices
PDF
Unit testing PHP apps with PHPUnit
OCP Java SE 8 Exam - Sample Questions - Java Streams API
Your code are my tests
Object Oriented Design Patterns for PHP
QA for PHP projects
Javascript Testing with Jasmine 101
Bring the fun back to java
Django for Beginners
Advanced PHPUnit Testing
PHP Security
Intro to testing Javascript with jasmine
Spring Certification Questions
Phpunit testing
Django, What is it, Why is it cool?
An introduction to SQLAlchemy
PhpUnit Best Practices
Unit testing PHP apps with PHPUnit
Ad

Viewers also liked (9)

PPT
BPR - Unit 4
PPT
PDF
PPT
PPTX
eBay Seller Standards & Defect Removal Policy - Seller Recharge Aug 2014
PDF
Reliability growth models for quality management
PPS
Framework For Smart E India Insyder
BPR - Unit 4
eBay Seller Standards & Defect Removal Policy - Seller Recharge Aug 2014
Reliability growth models for quality management
Framework For Smart E India Insyder
Ad

Similar to Jsp And Jdbc (20)

PPT
JDBC Java Database Connectivity
PPT
30 5 Database Jdbc
PDF
PPT
Executing Sql Commands
PPT
Executing Sql Commands
PDF
Basi Dati F2
PDF
java4th.pdf bilgisayar mühendisliği bölümü
PPT
Data Access with JDBC
PPT
JDBC for CSQL Database
DOCX
Db examples
PPT
JDBC – Java Database Connectivity
PPT
PDF
Jdbc[1]
PDF
JDBC programming
PPTX
PPS
Jdbc example program with access and MySql
PPTX
Database Access With JDBC
PDF
22jdbc
PDF
Introduction to JDBC and database access in web applications
PPT
KMUTNB - Internet Programming 6/7
JDBC Java Database Connectivity
30 5 Database Jdbc
Executing Sql Commands
Executing Sql Commands
Basi Dati F2
java4th.pdf bilgisayar mühendisliği bölümü
Data Access with JDBC
JDBC for CSQL Database
Db examples
JDBC – Java Database Connectivity
Jdbc[1]
JDBC programming
Jdbc example program with access and MySql
Database Access With JDBC
22jdbc
Introduction to JDBC and database access in web applications
KMUTNB - Internet Programming 6/7

More from Roy Antony Arnold G (20)

PDF
Quality management models
PDF
Pareto diagram
PDF
Ishikawa diagram
PDF
PDF
Customer satisfaction
PDF
Complexity metrics and models
PDF
Check lists
PDF
Capability maturity model
PDF
Structure chart
PDF
Seven new tools
PDF
Scatter diagram
PDF
Relations diagram
PDF
Rayleigh model
PDF
Defect removal effectiveness
PDF
Customer satisfaction
PDF
Complexity metrics and models
PDF
PDF
Seven basic tools of quality
Quality management models
Pareto diagram
Ishikawa diagram
Customer satisfaction
Complexity metrics and models
Check lists
Capability maturity model
Structure chart
Seven new tools
Scatter diagram
Relations diagram
Rayleigh model
Defect removal effectiveness
Customer satisfaction
Complexity metrics and models
Seven basic tools of quality

Recently uploaded (20)

PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
master seminar digital applications in india
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Institutional Correction lecture only . . .
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Cell Structure & Organelles in detailed.
PDF
RMMM.pdf make it easy to upload and study
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Insiders guide to clinical Medicine.pdf
Supply Chain Operations Speaking Notes -ICLT Program
2.FourierTransform-ShortQuestionswithAnswers.pdf
Microbial disease of the cardiovascular and lymphatic systems
Final Presentation General Medicine 03-08-2024.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
human mycosis Human fungal infections are called human mycosis..pptx
O5-L3 Freight Transport Ops (International) V1.pdf
master seminar digital applications in india
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
GDM (1) (1).pptx small presentation for students
Institutional Correction lecture only . . .
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Sports Quiz easy sports quiz sports quiz
Cell Structure & Organelles in detailed.
RMMM.pdf make it easy to upload and study
Abdominal Access Techniques with Prof. Dr. R K Mishra
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Insiders guide to clinical Medicine.pdf

Jsp And Jdbc

  • 1. Advanced Java Programming - Unit IV Roy Antony Arnold G Sr. Lecturer Einstein College of Engineering Tirunelveli, Tamilnadu, India
  • 2. Expressions.jsp <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> <HTML> <HEAD> <TITLE>JSP Expressions</TITLE> <META NAME=&quot;author&quot; CONTENT=&quot;Marty Hall&quot;> <META NAME=&quot;keywords” CONTENT=&quot;JSP,expressions,JavaServer,Pages,servlets&quot;> <META NAME=&quot;description” CONTENT=&quot;A quick example of JSP expressions.&quot;> <LINK REL=STYLESHEET HREF=&quot;JSP-Styles.css” TYPE=&quot;text/css&quot;> </HEAD> <BODY> <H2>JSP Expressions</H2> <UL> <LI>Current time: <%= new java.util.Date() %> <LI>Your hostname: <%= request.getRemoteHost() %> <LI>Your session ID: <%= session.getId() %> <LI>The <CODE>testParam</CODE> form parameter: <%= request.getParameter(&quot;testParam&quot;) %> </UL> </BODY> </HTML>
  • 4. Example for Scriptlets <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> <HTML> <HEAD> <TITLE>Color Testing</TITLE> </HEAD> <% String bgColor = request.getParameter(&quot;bgColor&quot;); boolean hasExplicitColor; if (bgColor != null) { hasExplicitColor = true; } else { hasExplicitColor = false; bgColor = &quot;WHITE&quot;; } %> <BODY BGCOLOR=&quot; <%= bgColor %>&quot;> <H2 ALIGN=&quot;CENTER&quot;>Color Testing</H2> <% if (hasExplicitColor) { out.println(&quot;You supplied an explicit background color of &quot; + bgColor + &quot;.&quot;); } else { out.println(&quot;Using default background color of WHITE. &quot; + &quot;Supply the bgColor request attribute to try a standard color, an RRGGBB value, or to see &quot; + &quot;if your browser supports X11 color names.&quot;); } %> </BODY> </HTML>
  • 5. JDBC There are seven standard steps in querying databases: 1. Load the JDBC driver. 2. Define the connection URL. 3. Establish the connection. 4. Create a statement object. 5. Execute a query or update. 6. Process the results. 7. Close the connection.
  • 6. 1. Load the JDBC Driver try { Class.forName(&quot;connect.microsoft.MicrosoftDriver&quot;); Class.forName(&quot;oracle.jdbc.driver.OracleDriver&quot;); Class.forName(&quot;com.sybase.jdbc.SybDriver&quot;); } catch(ClassNotFoundException cnfe) { System.err.println(&quot;Error loading driver: &quot; + cnfe); }
  • 7. 2. Define the Connection URL String host = &quot;dbhost.yourcompany.com&quot;; String dbName = &quot;someName&quot;; int port = 1234; String oracleURL = &quot;jdbc:oracle:thin:@&quot; + host + &quot;:&quot; + port + &quot;:&quot; + dbName; String sybaseURL = &quot;jdbc:sybase:Tds:&quot; + host + &quot;:&quot; + port + &quot;:&quot; + &quot;?SERVICENAME=&quot; + dbName;
  • 8. 3. Establish the Connection String username = “sample&quot;; String password = &quot;secret&quot;; Connection connection = DriverManager.getConnection(oracleURL, username, password); DatabaseMetaData dbMetaData = connection.getMetaData(); String productName = dbMetaData.getDatabaseProductName(); System.out.println(&quot;Database: &quot; + productName); String productVersion = dbMetaData.getDatabaseProductVersion(); System.out.println(&quot;Version: &quot; + productVersion);
  • 9. 4. Create a Statement Statement statement = connection.createStatement(); 5. Execute a Query String query = &quot;SELECT col1, col2, col3 FROM sometable&quot;; ResultSet resultSet = statement.executeQuery(query);
  • 10. 6. Process the Results while(resultSet.next()) { System.out.println(results.getString(1) + &quot; &quot; + results.getString(2) + &quot; &quot; + results.getString(3)); } 7. Close the Connection connection.close();
  • 11. Example 1 import java.sql.*; /** A JDBC example that connects to either an Oracle or a Sybase database and prints out the values of predetermined columns in the &quot;fruits&quot; table. */ public class FruitTest { public static void main(String[] args) { if (args.length < 5) { printUsage(); return; } String vendorName = args[4]; int vendor = DriverUtilities.getVendor(vendorName); if (vendor == DriverUtilities.UNKNOWN) { printUsage(); return; }
  • 12. String driver = DriverUtilities.getDriver(vendor); String host = args[0]; String dbName = args[1]; String url = DriverUtilities.makeURL(host, dbName, vendor); String username = args[2]; String password = args[3]; showFruitTable(driver, url, username, password); } /** Get the table and print all the values. */ public static void showFruitTable(String driver, String url, String username, String password) {
  • 13. try { // Load database driver if not already loaded. Class.forName(driver); // Establish network connection to database. Connection connection = DriverManager.getConnection(url, username, password); // Look up info about the database as a whole. DatabaseMetaData dbMetaData = connection.getMetaData(); String productName = dbMetaData.getDatabaseProductName(); System.out.println(&quot;Database: &quot; + productName); String productVersion = dbMetaData.getDatabaseProductVersion(); System.out.println(&quot;Version: &quot; + productVersion + &quot;\n&quot;); System.out.println(&quot;Comparing Apples and Oranges\n&quot; + &quot;================&quot;); Statement statement = connection.createStatement();
  • 14. String query = &quot;SELECT * FROM fruits&quot;; // Send query to database and store results. ResultSet resultSet = statement.executeQuery(query); // Look up information about a particular table. ResultSetMetaData resultsMetaData = resultSet.getMetaData(); int columnCount = resultsMetaData.getColumnCount(); // Column index starts at 1 (a la SQL) not 0 (a la Java). for(int i=1; i<columnCount+1; i++) { System.out.print(resultsMetaData.getColumnName(i) +&quot; &quot;); } System.out.println(); // Print results. while(resultSet.next()) {
  • 15. // Quarter System.out.print(&quot; &quot; + resultSet.getInt(1)); // Number of Apples System.out.print(&quot; &quot; + resultSet.getInt(2)); // Apple Sales System.out.print(&quot; $&quot; + resultSet.getFloat(3)); // Top Salesman System.out.println(&quot; &quot; + resultSet.getString(4)); } } catch(ClassNotFoundException cnfe) { System.err.println(&quot;Error loading driver: &quot; + cnfe); } catch(SQLException sqle) { System.err.println(&quot;Error connecting: &quot; + sqle); } } private static void printUsage() { System.out.println(&quot;Usage: FruitTest host dbName &quot; + &quot;username password oracle|sybase.&quot;); } } Java FruitTest db.ece fruitsale student xxxx oracle
  • 16. Example 2 import java.sql.*; /** Connect to Oracle or Sybase and print &quot;employees&quot; table. */ public class EmployeeTest { public static void main(String[] args) { if (args.length < 5) { printUsage(); return; } String vendorName = args[4]; int vendor = DriverUtilities.getVendor(vendorName); if (vendor == DriverUtilities.UNKNOWN) { printUsage(); return; } String driver = DriverUtilities.getDriver(vendor); String host = args[0]; String dbName = args[1]; String url = DriverUtilities.makeURL(host, dbName, vendor); String username = args[2]; String password = args[3]; DatabaseUtilities.printTable(driver, url, username, password, &quot;employees&quot;, 12, true); } private static void printUsage() { System.out.println(&quot;Usage: EmployeeTest host dbName &quot; + &quot;username password oracle|sybase.&quot;); } }