Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with database connectivity

 
sahana mithra
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is my code to just enter values in Employees table. Please let me know how to find if i have oracle driver in my system and how to find the driver name. The problem with my code is, it displays
org.apache.jasper.JasperException: An exception occurred processing JSP page /Testdb.jsp at line 14
error.

<%@ page import="java.sql.*" %>
<HTML>
<HEAD>
<TITLE>Filling a Table</TITLE>
</HEAD>

<BODY>
<H1>Filling a Table</H1>

<%
Connection connection = null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection("jdbc:odbc:Data", "system", "oracle");

Statement statement = connection.createStatement();

String command = "INSERT INTO Employees (ID, Name) VALUES (1, 'Joe')";
statement.executeUpdate(command);

command = "INSERT INTO Employees (ID, Name) VALUES (2, 'Yin')";
statement.executeUpdate(command);

ResultSet resultset =
statement.executeQuery("select * from Employees");

while(resultset.next()){
%>
<TABLE BORDER="1">
<TR>
<TH>ID</TH>
<TH>Name</TH>
</TR>
<TR>
<TD> <%= resultset.getString(1) %> </TD>
<TD> <%= resultset.getString(2) %> </TD>
</TR>
</TABLE>
<%
}
%>
</BODY>
</HTML>
 
Jagdeep Sharma
Ranch Hand
Posts: 121
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sahana mithra wrote:The following is my code to just enter values in Employees table. Please let me know how to find if i have oracle driver in my system and how to find the driver name. The problem with my code is, it displays
org.apache.jasper.JasperException: An exception occurred processing JSP page /Testdb.jsp at line 14
error.

<%@ page import="java.sql.*" %>
<HTML>
<HEAD>
<TITLE>Filling a Table</TITLE>
</HEAD>

<BODY>
<H1>Filling a Table</H1>

<%
Connection connection = null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection("jdbc:odbc:Data", "system", "oracle");

Statement statement = connection.createStatement();

String command = "INSERT INTO Employees (ID, Name) VALUES (1, 'Joe')";
statement.executeUpdate(command);

command = "INSERT INTO Employees (ID, Name) VALUES (2, 'Yin')";
statement.executeUpdate(command);

ResultSet resultset =
statement.executeQuery("select * from Employees");

while(resultset.next()){
%>
<TABLE BORDER="1">
<TR>
<TH>ID</TH>
<TH>Name</TH>
</TR>
<TR>
<TD> <%= resultset.getString(1) %> </TD>
<TD> <%= resultset.getString(2) %> </TD>
</TR>
</TABLE>
<%
}
%>
</BODY>
</HTML>



You above code doesn't use Oracle Driver, it uses JDBC-ODBC bridge driver. You can find Driver zip file(classes12.zip) in Oracle Installation Director. Then you can load JDBC Driver com.oracle.jdbc.Driver(type-4).
 
sahana mithra
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jagdeep,
Can you please tell me more about your reply. I mean what should i do now ? I am a very begginner to this . Can you please explain me the full procedure?
 
Swastik Dey
Rancher
Posts: 1637
6
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all avoid writing java codes in jsp, its a very old approach. It should only be used for presentation purpose/layer. Write your java codes in servlet and helper classes.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As this has nothing to do with JSP, it's been moved to the JDBC forum.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello sahana.

For each diferent database you use, you must have its conector. Oracle, MySQL. You must download it from internet and add to your app.
 
sahana mithra
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I downloaded oracle.jar. Where should I keep that jar file now? And how should I establish the connection? Where the code I have posted earlier is correct?
 
Paul Clapham
Sheriff
Posts: 21567
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are going to be professional about this, you would put the jar file in the classpath of the app server and configure a connection pool there which you can access via JNDI. But based on the fact that you planned to put your code into a JSP, it looks like you aren't in that kind of environment. In that case deploy the jar file to the WEB-INF/lib directory of your web application.

For establishing the connection, don't use that ODBC code. Load the class for the Oracle driver instead of the JDBC-ODBC driver, and use the JDBC URL for the Oracle driver instead of the jdbc:odbc URL. You should find information on that in the driver's documentation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic