Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
ryan needs these sql tests
  • Loading branch information
lwm14001 committed Nov 2, 2016
1 parent 31c68ee commit 4e9a930
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions sqlTest.java
@@ -0,0 +1,104 @@
//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;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/LocalStudentAdminDB");
con = ds.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM SA_Development.TERM");
PrintWriter out = response.getWriter();

String resultantJSON = "{";
ResultSetMetaData rsmd = rs.getMetaData();
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");
}

}
}

}

0 comments on commit 4e9a930

Please sign in to comment.