my sql database connection coding

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.
LVL 9
Manikandan ThiagarajanSenior consultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

radarshCommented:
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
radarshCommented:
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
Manikandan ThiagarajanSenior consultantAuthor Commented:
could you give me a link for that.

what is the use of mysqljconnector
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

radarshCommented:
If you are running from tomcat, do not forget put the mysql-connector JAR in the

WEB-INF\lib directory.

________
radarsh
0
radarshCommented:
http://www.mysql.com/products/connector/j/

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

________
radarsh
0
Manikandan ThiagarajanSenior consultantAuthor Commented:
>>3306 assuming your mysql is running on 3306 port.

how could i find out my sql is running this particular port
0
radarshCommented:
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
radarshCommented:
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
radarshCommented:
>> connection con = null;

Should be

Connection con = null; // note the Upper case C
________
radarsh
0
Manikandan ThiagarajanSenior consultantAuthor Commented:
<%@ 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
radarshCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Manikandan ThiagarajanSenior consultantAuthor Commented:
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
radarshCommented:
This is highly unrelevant. You have put some other jar file. Put mysql connector
jar in your classpath.

________
radarsh
0
radarshCommented:
And what on earth is AspectJ JAR file doing? How did you manage to get that error?

________
radarsh
0
Manikandan ThiagarajanSenior consultantAuthor Commented:
how could i set classpath to my connector jar file.

explain simple


0
Manikandan ThiagarajanSenior consultantAuthor Commented:
mysql-connector-java 5.0.0 b

this is my jar file name

is it correct
0
radarshCommented:
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
Manikandan ThiagarajanSenior consultantAuthor Commented:
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
radarshCommented:
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
Manikandan ThiagarajanSenior consultantAuthor Commented:
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
radarshCommented:
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
radarshCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.