?
Solved

my sql database connection coding

Posted on 2006-03-25
22
Medium Priority
?
824 Views
Last Modified: 2012-05-05
I have mysql 5.5

i create a database javatest.

i didnt assign username and password for my database.

table name is counter.

could you give me the database connection coding for mysql or

changs this jsp with msaccess  connection coding to mysql coding.

<%@ page language = "java" import ="java.sql.*" %>
<html>
<body>
<%

connection con = null;

try
{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con = DriverManager.getConnection("jdbc:odbc:ss","sa","");
      Statement st = con.createStatement();
      ResultSet rs = st.executeQuery("select * from Counter");
      %
            <table border =2>
            <%
            while(rs.next())
      {
            %>
            <tr>
            <td><%= rs.getString(1)%></td>

            <td><%= rs.getString(2)%></td>

            <td><%= rs.getString(3)%></td>

            </tr>

            <%
      }
            %>
            </table>
            <%
}
            catch(Exception d)
            {
                  out.println("can not display the records");

            }
            %>
            </body>
            </html>


Thanks in advance.
0
Comment
  • 14
  • 8
22 Comments
 
LVL 10

Expert Comment

by:radarsh
ID: 16288412
Fist download the mysql-j connector for MySQL. It will be a JAR file which you can find at
the mysql home page.


________
radarsh
0
 
LVL 10

Expert Comment

by:radarsh
ID: 16288425
change your code to

try
{
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ss");


// ss assumming your database is called ss. Change appropriately.
// 3306 assuming your mysql is running on 3306 port. Change otherwise.

Rest all remains the same.



_________
radarsh
0
 
LVL 9

Author Comment

by:Manikandan Thiagarajan
ID: 16288427
could you give me a link for that.

what is the use of mysqljconnector
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 10

Expert Comment

by:radarsh
ID: 16288429
If you are running from tomcat, do not forget put the mysql-connector JAR in the

WEB-INF\lib directory.

________
radarsh
0
 
LVL 10

Expert Comment

by:radarsh
ID: 16288436
http://www.mysql.com/products/connector/j/

Read on the above link for the use of this, etc, etc.

________
radarsh
0
 
LVL 9

Author Comment

by:Manikandan Thiagarajan
ID: 16288438
>>3306 assuming your mysql is running on 3306 port.

how could i find out my sql is running this particular port
0
 
LVL 10

Expert Comment

by:radarsh
ID: 16288443
Go to the folder where you've installed MySQL, find a file by name my.ini

In that file, there will be a line

# The TCP/IP Port the MySQL Server will listen on
3306

That number is the port.


________
radarsh
0
 
LVL 10

Expert Comment

by:radarsh
ID: 16288452
You also have to close the connection.
Put this in your code:

.......
.......
catch(Exception d)
{
    out.println("can not display the records");
}
finally
{
    if(con != null)
    {
        try
        {
            con.close();
        }
        catch(SQLException e)
        {
            out.println("Error closing the connection");
        }
    }
}
%>
</body>
</html>

________
radarsh
0
 
LVL 10

Expert Comment

by:radarsh
ID: 16288455
>> connection con = null;

Should be

Connection con = null; // note the Upper case C
________
radarsh
0
 
LVL 9

Author Comment

by:Manikandan Thiagarajan
ID: 16288744
<%@ page language = "java" import ="java.sql.*" %>
<html>
<body>
<%

Connection con = null;

try
{
        Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/javatest");

      Statement st = con.createStatement();
      ResultSet rs = st.executeQuery("select * from counter");
      <%
            <table border =2>
            <%
            while(rs.next())
      {
            %>
            <tr>
            <td><%= rs.getString(1)%></td>

            <td><%= rs.getString(2)%></td>

            <td><%= rs.getString(3)%></td>

            </tr>

            <%
      }
            %>
            </table>
            <%
}
            catch(Exception d)
            {
                  out.println("can not display the records");

            }

            finally
{
    if(con != null)
    {
        try
        {
            con.close();
        }
        catch(SQLException e)
        {
            out.println("Error closing the connection");
        }
    }
}
            %>
            </body>
            </html>


this program would give the error like this

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

An error occurred at line: 4 in the jsp file: /simplejdbc.jsp
Generated servlet error:
Syntax error on token(s), misplaced construct(s)

An error occurred at line: 4 in the jsp file: /simplejdbc.jsp
Generated servlet error:
Syntax error on token "%", invalid Type

An error occurred at line: 4 in the jsp file: /simplejdbc.jsp
Generated servlet error:
Syntax error on tokens, delete these tokens

An error occurred at line: 4 in the jsp file: /simplejdbc.jsp
Generated servlet error:
Syntax error, insert ";" to complete LocalVariableDeclarationStatement


how could i solve that error
0
 
LVL 10

Accepted Solution

by:
radarsh earned 300 total points
ID: 16288825
This should help

<%@ page language = "java" import ="java.sql.*" %>
<html>
<body>
<%

Connection con = null;

try
{
       Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/javatest");

     Statement st = con.createStatement();
     ResultSet rs = st.executeQuery("select * from counter");
%>
     <table border =2>

     <%
     while(rs.next())
     {
     %>
          <tr>
          <td><%= rs.getString(1)%></td>

          <td><%= rs.getString(2)%></td>

          <td><%= rs.getString(3)%></td>

          </tr>

    <%
     }
    %>
    </table>
<%
}
catch(Exception d)
{
   out.println("can not display the records");

}

finally
{
    if(con != null)
    {
        try
        {
            con.close();
        }
        catch(SQLException e)
        {
            out.println("Error closing the connection");
        }
    }
}
%>
</body>
</html>


________
radarsh
0
 
LVL 9

Author Comment

by:Manikandan Thiagarajan
ID: 16289181
now that would give the exception for your code

org.apache.jasper.JasperException: Exception in JSP: /simplejdbc.jsp:10

7:
8: try
9: {
10:        Class.forName("com.mysql.jdbc.Driver");
11:     con = DriverManager.getConnection("jdbc:mysql://localhost:3306/javatest");
12:
13:      Statement st = con.createStatement();


Stacktrace:
      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
      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

javax.servlet.ServletException: org/aspectj/lang/Signature
      org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:858)
      org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
      org.apache.jsp.simplejdbc_jsp._jspService(org.apache.jsp.simplejdbc_jsp:119)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
      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

java.lang.NoClassDefFoundError: org/aspectj/lang/Signature
      java.lang.Class.forName0(Native Method)
      java.lang.Class.forName(Unknown Source)
      org.apache.jsp.simplejdbc_jsp._jspService(org.apache.jsp.simplejdbc_jsp:52)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
      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.15 log
0
 
LVL 10

Expert Comment

by:radarsh
ID: 16289720
This is highly unrelevant. You have put some other jar file. Put mysql connector
jar in your classpath.

________
radarsh
0
 
LVL 10

Expert Comment

by:radarsh
ID: 16289783
And what on earth is AspectJ JAR file doing? How did you manage to get that error?

________
radarsh
0
 
LVL 9

Author Comment

by:Manikandan Thiagarajan
ID: 16291467
how could i set classpath to my connector jar file.

explain simple


0
 
LVL 9

Author Comment

by:Manikandan Thiagarajan
ID: 16291490
mysql-connector-java 5.0.0 b

this is my jar file name

is it correct
0
 
LVL 10

Expert Comment

by:radarsh
ID: 16292281
Did you put the .zip file itself under \WEB-INF\lib folder? In that case, it won't work. You have
to extract the zip folder and search for a mysql-connector-java-XXXX-bin.jar (XXXX can be any number).

Put this jar in your lib and let me know what happened.


________
radarsh
0
 
LVL 9

Author Comment

by:Manikandan Thiagarajan
ID: 16297631
Radarsh,

mysql-connector-java 5.0.0 b.jar i put in to webinf/lib

but same exception.

i didnt set classpath gor that jar file.

how could i set classpath.
0
 
LVL 10

Expert Comment

by:radarsh
ID: 16297825
Right click on the project, go to properties, go to Java Build Path click on add jars. Select your Jar from the lib folder
and add.

________
radarsh
0
 
LVL 9

Author Comment

by:Manikandan Thiagarajan
ID: 16297835
i didnt run from ecclipse.

could you please explain from step by step.

how could i create project folder , and add jsp file into that.

then how could i run jsp file from ecclipse.

please explain from start.

0
 
LVL 10

Expert Comment

by:radarsh
ID: 16297881
I had already explained all that in one of your previous questions.
If you really want to do that again, you might want to increase the
points for this question.

What say? :)

________
radarsh
0
 
LVL 10

Expert Comment

by:radarsh
ID: 16297899
Alright stmani,

Remove the mysql-connector-java-XXXX-bin-g.jar from your classpath
and just add the mysql-connector-java-XXXX-bin.jar

the -g jar is a debug version and should not be in your classpath

________
radarsh
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Applications for our next round of the Experts Exchange Scholarship Contest are starting to roll in. It made us wonder what our past winners are up to these days. Here's a look at what four winners experienced with the contest and what they're doing…
If anyone asked you to network diagram of the internet, it was drawn in the form of a fluffy cloud which further became known as cloud computing. Popularly cloud computing is defined as workloads that run over the internet in a commercial provider’s…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

850 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