Results 1 to 2 of 2
- 02-18-2011, 10:13 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
executing catch block by saying unable to connect to database
I am trying to retrieve data from database in atable
for that i created the jsp pages
the code is as follows
hi this my first page
welcome_to_database.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>*
<html>
<head>
<title>display data from the table using jsp</title>
</head>
<body>
<br><br><br><br><br><br><br>
<TABLE align="center" style="background-color: #ffffcc;">
<TR>
<TD align="center"><h2>To display all the data from the table click here...</h2></TD>
</TR>
<TR>
<TD align="center"><A HREF="ConnectJspToMysql.jsp">
<font size="4" color="blue">show data from table</font></A></TD>
</TR>
</TABLE>
</body>
</html>
and this is ConnectJspToMysql.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>*
<html>
<head>
<title>display data from the table using jsp</title>
</head>
<body>
<h2 align="center">Data from the table 'emp' of database 'empLeave12'</h2>
<%
try {
/* Create string of connection url within specified format with machine name, port number and database name. Here machine name id localhost and database name is student. */
String connectionURL = "jdbc:mysql://localhost:3306/empLeave12";
// declare a connection by using Connection interface
Connection connection = null;
// declare object of Statement interface that is used for executing sql statements.
Statement statement = null;
// declare a resultset that uses as a table for output data from tha table.
ResultSet rs = null;
//*Load JBBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").newInstance ();
/* Create a connection by using getConnection() method that takes parameters of string type connection url, user name and password to connect to database. */
connection = DriverManager.getConnection(connectionURL, "root", "notallowed");
/* createStatement() is used for create statement object that is used for sending sql statements
to the specified database. */
statement = connection.createStatement();
// sql query to retrieve values from the secified table.
String QueryString = "SELECT * from emp";
rs = statement.executeQuery(QueryString);
%>
<TABLE cellpadding="15" border="1" style="background-color: #ffffcc;" align="center">
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getInt(1)%></TD>
<TD><%=rs.getString(2)%></TD>
<TD><%=rs.getString(3)%></TD>
<TD><%=rs.getString(4)%></TD>
</TR>
<% } %>
<%
// close all the connections.
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
%>
</font>
<font size="+3" color="red"></b>
<%
out.println("Unable to connect to database.");
}
%>
</TABLE><TABLE align="center">
<TR>
<TD><FORM ACTION="welcome_to_database_query.jsp" method="get" >
<button type="submit"><-- back</button></TD>
</TR>
</TABLE>
</font>
</body>
</html>
when i run these in browser whatever there in the catch block is executing
can any one give the reply
- 02-18-2011, 10:27 AM #2
First you must you use special tag code /code.
Second I should recommend you divide code on the MVC, i.e. you can take out code which retrieve data from database in special class and used it in jsp.Skype: petrarsentev
http://TrackStudio.com
Similar Threads
-
unable to connect remote database using jdbc
By enggvijaysingh@gmail.com in forum Advanced JavaReplies: 1Last Post: 12-16-2010, 12:16 PM -
Unable to connect to remote mysql database using IP Address
By kranthipoturaju in forum JDBCReplies: 4Last Post: 04-28-2010, 06:13 PM -
Try/catch block
By swati.jyoti in forum New To JavaReplies: 5Last Post: 07-02-2009, 02:32 PM -
Question reg try/catch block
By nn12 in forum New To JavaReplies: 1Last Post: 09-16-2008, 05:56 PM -
try...catch block
By javaplus in forum New To JavaReplies: 3Last Post: 11-06-2007, 07:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks