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

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;
    }
}

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

Read Text File (.txt) Using JSP / Java

<%
BufferedReader input = new BufferedReader(new FileReader("text2read.txt"));
String line = "";
while ((line = input.readLine()) != null) {
out.println(line);
}
output.flush();
input.close();
%>

Source: http://experts.about.com/q/JSP-Java-Server…xt-file-JSP.htm
Additional example http://www.jguru.com/forums/view.jsp?EID=536740