Solved

jdbc/mysql first example not working

Posted on 2007-03-18
26
1,077 Views
Last Modified: 2008-01-09
Hi All,
I got this first  script for me using jdbc/mysql:
simply it does not work
it displays:
Ooops, something bad happened:,

I'm using mysql-connector-java-3.0.1 beta  it's file is   mysql-connector-java-3.0.1-beta-bin.jar file  in my %java_home%jre/lib/ext/ directory
the only change is in my user name and password
and  I'm sure it's not the problem

any hint, help, Idea or free mysql/jdbc driver that is stable link  or modification in code is highly welcomed
Best Regards
HG

here is the code

<HTML>
 <HEAD><TITLE>Employee List</TITLE></HEAD>
 <BODY>
<%@ page import="java.sql.*" %>
<TABLE BORDER=1 width="75%">
<TR><TH>Last Name</TH><TH>First Name</TH></TR>
<%
//Connection conn = null;
java.sql.Connection conn= null;
Statement st = null;
ResultSet rs = null;

try {
//    Class.forName("com.mysql.jdbc.Driver").newInstance();
      Class.forName("org.gjt.mm.mysql.Driver").newInstance();
    conn = DriverManager.getConnection(
            "jdbc:mysql://localhost/jsp?user=xxx&password=xxx");

    st = conn.createStatement();
    rs = st.executeQuery("select * from employees");
    while(rs.next()) {
%>
<TR><TD><%= rs.getString("lname_txt") %></TD>
<TD><%= rs.getString("fname_txt") %></TD></TR>
<%
     }
%>
</TABLE>
<%
}  catch (Exception ex) {
    ex.printStackTrace();
    %>
</TABLE>
Ooops, something bad happened:
<%
    }  finally {
    if (rs != null) rs.close();
    if (st != null) st.close();
    if (conn != null) conn.close();
    }
%>
</BODY>
</HTML>

0
Comment
Question by:shang3000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
  • 7
  • +2
26 Comments
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 250 total points
ID: 18744525
>> Ooops, something bad happened:

Do you see any stack trace in the server console or logs for the exception that occured? You can also use: ex.printStackTrace ( out ) ; to display it.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 250 total points
ID: 18744527
BTW the call to newInstance () is not required - just use Class.forName ()
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 18744541
Great -- does this mean that you got Tomcat working (earlier EE question)?

If you posted the printStackTrace, that would pinpoint the problem.

This code says that you have a Web server localhost with a mysql database file in its root directory named jsp -- is that right?

Again, without the stack trace it's hard to tell what the problem is.  Are you running from within an IDE, and do you have the driver in your IDE environment?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:mrcoffee365
ID: 18744547
Sorry maynkeagle, didn't see your post.  Wouldn't want to poach on your question.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18744569
sorry my comment somehow went as Proposed solution - sometimes this new UI of EE creates problems.
0
 
LVL 23

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 50 total points
ID: 18745865
> ex.printStackTrace();

What does this print?
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 250 total points
ID: 18745880
>> Sorry maynkeagle, didn't see your post.  Wouldn't want to poach on your question.

Its not 'my' question - obviously anybody can post on it and help! I understand you posted at almost the same time as me so if you repeated something like stack trace, there's no problem.
0
 

Author Comment

by:shang3000
ID: 18746598
>>Great -- does this mean that you got Tomcat working (earlier EE question)?
Hi mrcoffee365,
this on windows the earlier question was on linux
0
 

Author Comment

by:shang3000
ID: 18746601
Hi mayankeagle,
>>ex.printStackTrace ( out ) ; to display it
here is the stack trace I hope you can help me
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 29 in the jsp file: /jsp/db.jsp
Generated servlet error:
The method printStackTrace() in the type Throwable is not applicable for the arguments (JspWriter)


      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 29 in the jsp file: /jsp/db.jsp
Generated servlet error:
The method printStackTrace() in the type Throwable is not applicable for the arguments (JspWriter)


      org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
      org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
      org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:414)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:297)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:276)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:264)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:305)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.20 logs.


--------------------------------------------------------------------------------
0
 

Author Comment

by:shang3000
ID: 18746609
Hi mrcoffee365,
>>This code says that you have a Web server localhost with a mysql database file in its root directory named jsp -- is that right?

I'm new to jdbc  I think  it means I have database server on localhost it's type mysql and the database name is  jsp
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 200 total points
ID: 18748453
>>>ex.printStackTrace ( out ) ; to display it  
Instead try  
out.print(ex.toString());
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 200 total points
ID: 18748543
I think mrcoffee was hinting that you should close some of your old questions.  If you had fewer questions open then the experts would probably spend more time helping you.
0
 

Author Comment

by:shang3000
ID: 18757213
Hi rrz@871311,
>>Instead try  
out.print(ex.toString());
I tried this class :
Class.forName("com.mysql.jdbc.Driver").newInstance();
and I get this error
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I tried this class :
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
and I get this error
java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver
can you please help me to fix what is wrong with the code or the driver or guide me to another driver that works fine with java, servlets and jsp and mysql database
Best Regards
HG

0
 

Author Comment

by:shang3000
ID: 18757238
Hi Ajay-Singh,
>> ex.printStackTrace();
>> What does this print?
it prints this:
type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 29 in the jsp file: /jsp/db.jsp
Generated servlet error:
The method printStackTrace() in the type Throwable is not applicable for the arguments (JspWriter)


      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 29 in the jsp file: /jsp/db.jsp
Generated servlet error:
The method printStackTrace() in the type Throwable is not applicable for the arguments (JspWriter)


      org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
      org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
      org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:414)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:297)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:276)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:264)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:305)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.20 logs.


0
 

Author Comment

by:shang3000
ID: 18757289
Hi rrz@871311,
>> I think mrcoffee was hinting that you should close some of your old questions.  If you had fewer
>> questions open then the experts would probably spend more time helping you.
thank you for your kind notice but the problem infact is:
I'm trying java on linux and till now no good so I switched to window till I find a solution to my problem
that's why I need the experts help me  in both platforms Linux and windows
so if any expert have any idea it would be greate:
Best Regards
HG
0
 

Author Comment

by:shang3000
ID: 18757996
Hi rrz@871311,
I'm sorry please ignore  the last error I told you about it:
this is the last error I got:
java.sql.SQLException: Error during query: Unexpected Exception: java.lang.ArrayIndexOutOfBoundsException message given: 35

here is the source code again to facilitate on you :
-----------------------------------------------------------------
<HTML>
 <HEAD><TITLE>Employee List</TITLE></HEAD>
 <BODY>
<%@ page import="java.sql.*" %>
<TABLE BORDER=1 width="75%">
<TR><TH>Last Name</TH><TH>First Name</TH></TR>
<%
//Connection conn = null;
java.sql.Connection conn= null;
Statement st = null;
ResultSet rs = null;

try {
          Class.forName("com.mysql.jdbc.Driver").newInstance();
            //Class.forName("org.gjt.mm.mysql.Driver").newInstance();
          conn = DriverManager.getConnection(
            "jdbc:mysql://localhost/jsp?user=root&password=hgts2006");

          st = conn.createStatement();
          rs = st.executeQuery("select * from employees");
          while(rs.next())
            {
%>
<TR><TD><%= rs.getString("lname_txt") %></TD>
<TD><%= rs.getString("fname_txt") %></TD></TR>
<%
           };
%>
</TABLE>
<%
      }  
      catch (Exception ex)
      {
          // ex.printStackTrace(out);
            out.print(ex.toString());

    %>
</TABLE>
Ooops, something bad happened:
<%
    }  
      finally
      {
          if (rs != null) rs.close();
               if (st != null) st.close();
          if (conn != null) conn.close();
    }
%>
</BODY>
</HTML>
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 200 total points
ID: 18760619
I am not an expert on databases. Hopefully an expert will help.  But, as I said before, when you have 12 open questions the experts hesitate to help. They think you will just abandon this question as well.  
>java.sql.SQLException: Error during query: Unexpected Exception: java.lang.ArrayIndexOutOfBoundsException message given: 35  
All I guess is that your table in your database doesn't have the two columns. Can you use mysql at the command line and see what is  the employees table ?  
0
 

Author Comment

by:shang3000
ID: 18762722
Hi rrz@871311,
>>All I guess is that your table in your database doesn't have the two columns. Can you use mysql at the >>command line and see what is  the employees table ?  

this the result :
mysql> use jsp;
Database changed
mysql> select * from employees;
+-----------+-----------+--------------+-----------------------+--------------+-
------------+-------+------------+----------------+
| lname_txt | fname_txt | employee_num | address1_txt          | address2_txt |
city        | state | zip        | phone          |
+-----------+-----------+--------------+-----------------------+--------------+-
------------+-------+------------+----------------+
| Jones     | Bob       |            1 | 27 Mockingbird Lane   | fgg          |
Springfield | MA    | 11223-4321 | 1-617-555-1212 |
| Smith     | John      |            2 | 55 Boring Street Name | Apt 1        |
Roswell     | NM    | 65444-4556 | 1-800-AMA-LIEN |
+-----------+-----------+--------------+-----------------------+--------------+-
------------+-------+------------+----------------+
2 rows in set (0.08 sec)

mysql> select * from employees;
+-----------+-----------+--------------+-----------------------+--------------+-
------------+-------+------------+----------------+
| lname_txt | fname_txt | employee_num | address1_txt          | address2_txt |
city        | state | zip        | phone          |
+-----------+-----------+--------------+-----------------------+--------------+-
------------+-------+------------+----------------+
| Jones     | Bob       |            1 | 27 Mockingbird Lane   | fgg          |
Springfield | MA    | 11223-4321 | 1-617-555-1212 |
| Smith     | John      |            2 | 55 Boring Street Name | Apt 1        |
Roswell     | NM    | 65444-4556 | 1-800-AMA-LIEN |
+-----------+-----------+--------------+-----------------------+--------------+-
------------+-------+------------+----------------+
2 rows in set (0.00 sec)

I'm also seeing the data from phpmyadmin

could it be  error in the driver

Best Regards
HG
0
 

Author Comment

by:shang3000
ID: 18762970
thanks guys for your help it worked
the problem is in the driver it is  a beta version

I solved the problem by downloading the the j-connector verison 5.0 from mysql site
and install and configure it so it work
refere to the following url for more details

http://dev.mysql.com/downloads/connector/ : for download
http://dev.mysql.com/doc/refman/5.0/en/connector-j.html  :for documentation
any way guys I'm very gratfull for your help as the best as you can

Best Regards :)
HG
0
 
LVL 27

Expert Comment

by:rrz
ID: 18764152
Thanks for closing, shang.  But, the first comment  made by mayankeagle is bad code.  You should not  have given points for such a comment. I am sorry that we didn't suggest that you get a better driver early in this discussion.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18768316
Which one? ex.printStackTrace ( out ) ; ? That was just for debugging with minimal changes to the existing code, not a production code anyway
0
 
LVL 27

Expert Comment

by:rrz
ID: 18768664
>Which one? ex.printStackTrace ( out ) ; ?  
Come on, you know that would never work.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18768673
Why not if out is the standard output stream writer?
0
 
LVL 27

Expert Comment

by:rrz
ID: 18768711
>Why not if out is the standard output stream writer?  
java.lang.Throwable  has these two method signatures
public void printStackTrace(PrintStream s)  
public void printStackTrace(PrintWriter s)  
"out" is a JspWriter  which has this hierarchy
java.lang.Object
 - java.io.Writer
     - javax.servlet.jsp.JspWriter  
Thus,  we get  
>Generated servlet error:
>The method printStackTrace() in the type Throwable is not applicable for the arguments (JspWriter)    
as posted by shang3000 above here.    Do you agree ?

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18768744
Sorry I meant it like the standard output stream writer out = new PrintWriter ( System.out ) ;
0
 
LVL 27

Expert Comment

by:rrz
ID: 18768756
I see,  now that makes sense.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here's how to start interacting with our community through Post.
Let’s face it: one of the reasons your organization chose a SaaS solution (whether Microsoft Dynamics 365, Netsuite or SAP) is that it is subscription-based. The upkeep is done. Or so you think.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question