Skip to content

Ozar.net Developer Blog

Programming in PHP, C, C#, ASP.NET, Java, Objective-C, SQL also for MS SQL Server, Oracle & MySQL Development

Menu
  • Home
  • About
  • Tutorials
  • Contact
  • Privacy
Menu

Example with Source Code: A simple database-driven Java web application (CRUD) implementing JSP and Servlets

Posted on March 28, 2010February 1, 2011 by OD

You can study and implement the following examples to create a simple Java-based database-driven web application which makes use of JSP and Servlets applying a MVC (model-view-controller) principle.

Create the following files:

DatabaseConnection.java

package sample;
import java.sql.*;

public class DatabaseConnection {

    public static Connection getConnection() {
    Connection con = null;
    try {
        // change the following line to your jdbc driver
        Class.forName("com.borland.datastore.jdbc.DataStoreDriver");
        // change this to "your database url", "username", "password"
        con = DriverManager.getConnection("jdbc:borland:dslocal:C:\\a.jds","Sample","");
        System.out.println(con);
        }
        catch (Exception ex) {
        ex.printStackTrace();
        }
    return con;
    }
}

DataBean.java

package sample;

import java.util.*;
import java.sql.*;

public class DataBean {

    // Constructor:
    public DataBean() {
    }

    public Vector listAll() {
    Vector result = new Vector();
    try {
        Connection con = DatabaseConnection.getConnection();
        Statement stmt = con.createStatement();
        System.out.println(stmt);
        ResultSet rs = stmt.executeQuery("select * from table1");
        while (rs.next()) {
            Table1Bean bean = new Table1Bean();
            bean.setId(rs.getInt("id"));
            bean.setName(rs.getString("name"));
            result.add(bean);
        }
    }
        catch (SQLException ex) {
        ex.printStackTrace();
    }
    return result;
    }
}

Table1Bean.java

package sample;

public class Table1Bean {

    private int id;
    private String name;

    public Table1Bean() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

display.jsp

 <%@ page contentType="text/html; charset=UTF-8" language="java" errorPage="" %>
 <%@ page import="sample.*"%>
 <%@ page import="java.util.*"%>
 <%
    DataBean dataBean = new DataBean();
    Vector list = dataBean.listAll();
 %>
 <html>
 <head>
    <title>Display Data</title>
 </head>
 <body bgcolor="#ffffff">
    <table border="1">
       <tr>
          <th>ID</th>
          <th>NAME</th>
       </tr>
       <%
          for(int i=0; i<list.size(); i++) {
             Table1Bean bean = (Table1Bean)list.elementAt(i);
       %>
       <tr>
          <td><%=bean.getId()%></td>
          <td><%=bean.getName()%></td>
       </tr>
       <%
          }
        %>
    </table>
 </body>
 </html>

Leave a Reply Cancel reply

You must be logged in to post a comment.

Recent Posts

  • NSTableView with Core Data Tutorial using NSFetchedResultsController
  • CodeIgniter Wizard – a new PHP CRUD Code Generator for macOS
  • Installing Oracle Database 10g Express Edition and Changing the Default HTTP Port
  • Xcode Tutorial 2 – Putting iAd into your App (The Apple Approved Way)
  • Cocoa Programming: A Quick-Start Guide for Developers

Blogroll

  • All Systems GO
  • GOZmosis
  • Maclord's Blog
  • Ozar.net

Tags

.NET 2008 application ASP.NET basic Beginning best books business Business Objects C# Database Databases Development Edition Guide iPhone Java Java EE learn Learning Microsoft MS SQL Server MySQL Objective-C Programming objects Oracle Part PHP PL/SQL Professional Programming Server Software SQL T-SQL TSQL Tutorial Tutorials Using Video Visual Visual Basic .NET Web Programming Tutorials XCode
© 2026 Ozar.net Developer Blog | Powered by Minimalist Blog WordPress Theme

Powered by
►
Necessary cookies enable essential site features like secure log-ins and consent preference adjustments. They do not store personal data.
None
►
Functional cookies support features like content sharing on social media, collecting feedback, and enabling third-party tools.
None
►
Analytical cookies track visitor interactions, providing insights on metrics like visitor count, bounce rate, and traffic sources.
None
►
Advertisement cookies deliver personalized ads based on your previous visits and analyze the effectiveness of ad campaigns.
None
►
Unclassified cookies are cookies that we are in the process of classifying, together with the providers of individual cookies.
None
Powered by