JDBC servlet-Database Connection

In JDBCInitServlet (Example 1 and Example 2) Connection to a DB2 database from the variables we initialize from the servlet configuration f...


In JDBCInitServlet (Example 1 and Example 2) Connection to a DB2 database from the variables we initialize from the servlet configuration file. This example demonstrates how to make a connection to an external resource, and print the results back in the response.

Example 1 - Connecting to a JDBC Database
package itso.servjsp.servletapi;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class JDBCInitServlet extends SimpleInitServlet {
protected Connection conn = null;
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
// load JDBC driver
Class.forName(mydriver).newInstance();
conn = DriverManager.getConnection(myurl, myuserID, mypassword);
System.out.println("Connection successful..");
}
catch (SQLException se) { System.out.println(se); }
catch (Exception e) { e.printStackTrace(); }
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("TEXT/HTML");
PrintWriter out = res.getWriter();
out.println("<HTML>");
out.println("<TITLE>JDBC Init Connection</TITLE>");
out.println("<BODY>");
try { executeSQL(out); }
catch (SQLException se) { se.printStackTrace(); }
out.println("</BODY></HTML>");
out.close();
}

Example 2 - Sql access
public void executeSQL(PrintWriter out) throws SQLException{
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM DEPARTMENT";
stmt.executeQuery(sql);
ResultSet rs = stmt.getResultSet();
int count = 1;
while (rs.next()) {
out.println("<B>"+rs.getString("DEPTNAME")+"</B><BR><BLOCKQUOTE>");
String sql2 = "SELECT * FROM EMPLOYEE WHERE WORKDEPT = '" +
rs.getString("DEPTNO") + "'";
Statement stmt2 = conn.createStatement();
stmt2.executeQuery(sql2);
ResultSet rs2 = stmt2.getResultSet();
while(rs2.next()) {
out.println(rs2.getString("FIRSTNME") + " " +
rs2.getString("LASTNAME") + "<br>");
}
out.println("</BLOCKQUOTE>");
}
}}

The JDBCInitServlet extends the SimpleInitServlet. This demonstrates that we can consider designing base servlet classes at an application level and then extend them for a specific function. This servlet was built primarily to demonstrate functionality; exception handling has been left out in order to keep the code concise.

We choose to extend the SimpleInitServlet, and override the doGet method to make our processing specific to this example. The executeSQL method performs the actual SQL database calls.
This servlet connects to the SAMPLE database that is installed with DB2. It must first load up the database driver and make the connection. In this case, we choose to make the connection object (Connection conn) a shared instance variable, which we reuse from servlet request to servlet request, but initialize only once.

The connection information (user ID, password, URL, and driver) is specified in the JDBCInitServlet.servlet file, which must be copied to the appropriate directory.
Name

ADO,131,ASP,3,C++,61,CORE JAVA,1,CSS,115,HTML,297,index,5,JAVASCRIPT,210,OS,47,PHP,65,SAD,53,SERVLETS,23,SOFTWARE ENGINEERING,245,SQL,71,TCP/IP,1,XHTML,9,XML,18,
ltr
item
Best Online Tutorials | Source codes | Programming Languages: JDBC servlet-Database Connection
JDBC servlet-Database Connection
Best Online Tutorials | Source codes | Programming Languages
https://www.1000sourcecodes.com/2012/05/jdbc-servlet-database-connection.html
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/2012/05/jdbc-servlet-database-connection.html
true
357226456970214079
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content