DATA BASE CONNECTIVITY USING JSP
AIMTo write a JSP program to retrieve the employee details from Tomcat apache server.
REQUIREMENTS:
1. MySQL
2. Tomcat - version 4.1.12 Standard used for this tutorial
3. Java 2 JRE - version 1.4.1 used for this tutorial
4. MySQL Connector/J - version 2 used for this tutorial
INSTALLATION
1. Install the Java 2 JRE. I put mine in C:\java\jre, which will be used in this tutorial, but you can put your anywhere you like.
2. Extract the Tomcat distribution files to a folder. The default is jakarta-tomcat-4.1.12, but I chose to put the files in C:\Tomcat.
3. Copy the MySQL Connector JAR file to the C:\Tomcat\common\lib folder (ie, mysql-connector-java-2.0.14.jar).
ENVIRONMENTAL VARIABLES
Add the following environmental variables to Windows:
JAVA_HOME=C:\java\jre
TOMCAT_HOME=C:\Tomcat
set environmental variables in Windows 2000/XP by going to:
Righy-click My Computer -> Properties -> Advanced -> Environmental Variables
WORKING WITH TOMCAT
To start the server, execute startup.bat. To stop the server, execute shutdown.bat in the C:\Tomcat\bin folder.
By default, the Tomcat web server is access at this URL:
http://localhost:8080/
The root folder of the server is located in:
C:\Tomcat\webapps\ROOT
The root and default port can be changed in this file:
C:\Tomcat\conf\server.xml
PROGRAM
//db.jsp
<%@ page import="java.sql.*" %>
<%
String connectionURL = "jdbc:mysql://localhost:3306/mydatabase?user=;password=";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
%>
<html><body>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "", "");
statement = connection.createStatement();
rs = statement.executeQuery("SELECT * FROM DEPT");
while (rs.next()) {
out.println(rs.getString("2")+"<br>");
}
rs.close();
%>
</body></html>
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
91 marketing san diego
93 finance london
60 MIS NEW YORK
50 development detroit
70 FINANCE
80 research houston
10 rows selected.
0 comments:
Post a Comment