Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
//package com.journaldev.jdbc.datasource;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
//@WebServlet("/JDBCDataSourceExample")
@WebServlet("/Test")
public class sqlTest extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Context ctx = null; //context to pull var names from
Connection con = null; //initializes connection
Statement stmt = null; //initializes statement you send for sql
ResultSet rs = null; //initializes resultSet for sql
try{
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/LocalStudentAdminDB"); //jdbc lookup
con = ds.getConnection(); //connection from data source
stmt = con.createStatement(); //creates empty statement
rs = stmt.executeQuery("SELECT * FROM SA_Development.TERM"); //results from sql query
PrintWriter out = response.getWriter();
String resultantJSON = "["; //
ResultSetMetaData rsmd = rs.getMetaData(); //all information that isn't the result #cols #rows
int columnCount = rsmd.getColumnCount();
while(rs.next()){
String individualRow = "{";
for(int i=1; i < columnCount + 1;i++){
String name = rsmd.getColumnName(i);
if(rsmd.getColumnType(i)==java.sql.Types.INTEGER){
individualRow += "'" + name + "': '" + rs.getInt(name) + "',";
} else if (rsmd.getColumnType(i)==java.sql.Types.DATE){
individualRow += "'" + name + "': '" + rs.getDate(name) + "',";
} else if (rsmd.getColumnType(i)==java.sql.Types.VARCHAR){
individualRow += "'" + name + "': '" + rs.getString(name) + "',";
} else {
individualRow += "'" + name + "': '" + rs.getDate(name) + "',";
}
}
resultantJSON += individualRow + "},";
}
resultantJSON += "]";
response.setContentType("application/json");
out.print(resultantJSON);
/* response.setContentType("text/html");
out.print("<html><body><h2>SQLResults</h2>");
out.print("<table border=\"1\" cellspacing=10 cellpadding=5>");
out.print("<th>TERM_ID</th>");
out.print("<th>TERM_NAME</th>");
out.print("<th>ACTIVE_DATE_FROM</th>");
out.print("<th>ACTIVE_DATE_TO</th>");
while(rs.next())
{
out.print("<tr>");
out.print("<td>" + rs.getInt("TERM_ID") + "</td>");
out.print("<td>" + rs.getString("TERM_NAME") + "</td>");
out.print("<td>" + rs.getDate("ACTIVE_DATE_FROM") + "</td>");
out.print("<td>" + rs.getDate("ACTIVE_DATE_TO") + "</td>");
out.print("</tr>");
out.print(rs
}
out.print("</table></body><br/>");
//lets print some DB information
//out.print("<h3>Database Details</h3>");
//out.print("Database Product: "+con.getMetaData().getDatabaseProductName()+"<br/>");
//out.print("Database Driver: "+con.getMetaData().getDriverName());
//out.print("</html>");
*/
}catch(NamingException e){
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
stmt.close();
con.close();
ctx.close();
} catch (SQLException e) {
System.out.println("Exception in closing DB resources");
} catch (NamingException e) {
System.out.println("Exception in closing Context");
}
}
}
}