Hi.. I am trying to connect to my database (MySQL) within a JSP page.
The is compiling without any error, but still I am not getting any output.
Can anybody help??
The code is as follows:
<%@page contentType="text/html" pageEncoding="windows-1252"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>Enter to database</title></head>
<body>
<table>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%
java.sql.Connection connecton=null;
java.sql.Statement stmt=null;
java.sql.PreparedStatement prepStmt=null;
ResultSet rs = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
connecton=DriverManager.getConnection("jdbc:mysql://localhost:3306/project_demo","Sops","adminadmin");
stmt=connecton.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
String sql = "select * from test";
try{
//stmt = DBConnection.getStatement();
rs = stmt.executeQuery(sql);
%>
<%
while( rs.next() ){
%><tr>
<td><%= rs.getString("Question") %></td>
</tr>
<tr>
<td><%= rs.getString("Answer1") %></td>
</tr>
<tr>
<td><%= rs.getString("Answer2") %></td>
</tr>
<tr>
<td><%= rs.getString("Answer3") %></td>
</tr>
<tr>
<td><%= rs.getString("Answer4") %></td>
</tr>
<%
}
%>
<%
}
catch(Exception e){e.printStackTrace();}
finally{
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(connecton!=null) connecton.close();
}
%>
</table>
</body>
</html>