iTech College - Internet Technology, Online Education, E-Learning, Technician Training, Tech Tutorials, Distance Education, Virtual Learning JSP Course: Use Database - www.itechcollege.com JSP Course: Use Database - www.itechcollege.com
Free Courses
HTML
JavaScript
CSS
ASP
PHP
JSP
C++
ASP.NET
SQL
AJAX
FTP
XML

courses > JSP > Use Database

Use Database

First you need to create a database. Let's use MS Access. Make sure you have MS Access installed on your computer and you know how to configure it.

in Windows 95/98. you should go into the ODBC32 icon in your control panel, and add MS Access Drive on System DSN. Then create a database and a table. You must give a name to your database and to the table. Let's name the database "my_db.mdb", name the table "members" and the path to this database is:

C:\Inetpub\wwwroot\my_db.mdb

Let's design a table "members" something like this:

userId user pass page
00234578 John John123 john.JSP
-11350986 Mike Mike456 mike.JSP
98782345 Joe Joe789 joe.JSP
... ... ... ...

To open the database, we need the following code.
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:my_db"); Statement stmt = con.createStatement();
%>
Now you have established a connection to the database. To get data from the database, we can use SQL.

The next statement is combined JSP code and SQL:

String query="select * from members";
ResultSet rs = stmt.executeQuery(query);
You can write records from the database to your web browser;
while(rs.next()){
String user=rs.getString("user");
String pass=rs.getString("pass");
String page=rs.getString("page");
out.println user +", "+pass+", "+page+"<br>";
}

The meaning of the above code is: if the search "rs" does not reach the end of the table, keep writing the records on web browser. When you execute the script, all the records of "user" and "pass" will be retriaved. The below is a complete JSP page using database. Let's save it as second.JSP:

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %> <% try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:my_db"); Statement stmt = con.createStatement(); String query="select * from members"; ResultSet rs = stmt.executeQuery(query); while(rs.next()){
String user=rs.getString("user");
String pass=rs.getString("pass");
String page=rs.getString("page");
out.println user +", "+pass+", "+page+"<br>";
} }catch (Exception e) { out.println("<br><p align=center><font color=red>Connection can not be created.</font></p>"); } %>


  Back to Index Page
 Next Page:  

Introduction
JSP Tags and Include File
println and getParameter
The If Statement
For Loop
Redirect
Session
Use Database
JSP syntax
Scripting elements
Actions


Online Education, Online Training for Your Job, Distance Education, E-learning Programs for Your Career!Online Colleges, Online Universities, E-learning & Distance Education for Your Career! Easy Job Training & Online Courses.


   Email A Friend  
 
Earn Online Degree
for Your Job
I found a job after I earned an online degree!
Online classes for finding a Job. Online College, Online University, Degrees, E-learning school, online certificate Earn your
online degree now!
Online Training
for Good Job
Why I work so hard but earn so little? Why I cannot find a high paying job?
Online training for a Good Jobs. Online Degree, E-learning school, online certificate Go to
online school now!

Copyright ©2024, itechcollege.com. All rights reserved
iTechCollege | About iTech | Contact iTech