-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
||
} | ||
} | ||
|
||
} |