Solved

jdbc/mysql first example not working

Posted on 2007-03-18
26
1,061 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
  • 9
  • 7
  • 7
  • +2
26 Comments
 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle 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:
mayankeagle earned 250 total points
ID: 18744527
BTW the call to newInstance () is not required - just use Class.forName ()
0
 
LVL 26

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
 
LVL 26

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:mayankeagle
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:mayankeagle
mayankeagle 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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:mayankeagle
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:mayankeagle
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:mayankeagle
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
For cloud, the “train has left the station” and in the Microsoft ERP & CRM world, that means the next generation of enterprise software from Microsoft is here: Dynamics 365 is Microsoft’s new integrated business solution that unifies CRM and ERP fun…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now