We help IT Professionals succeed at work.

my sql database connection coding

Manikandan Thiagarajan
on
Medium Priority
871 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.
Comment
Watch Question

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


________
radarsh

Commented:
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
Manikandan ThiagarajanSenior consultant

Author

Commented:
could you give me a link for that.

what is the use of mysqljconnector

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

WEB-INF\lib directory.

________
radarsh

Commented:
http://www.mysql.com/products/connector/j/

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

________
radarsh
Manikandan ThiagarajanSenior consultant

Author

Commented:
>>3306 assuming your mysql is running on 3306 port.

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

Commented:
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

Commented:
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

Commented:
>> connection con = null;

Should be

Connection con = null; // note the Upper case C
________
radarsh
Manikandan ThiagarajanSenior consultant

Author

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
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Manikandan ThiagarajanSenior consultant

Author

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

Commented:
This is highly unrelevant. You have put some other jar file. Put mysql connector
jar in your classpath.

________
radarsh

Commented:
And what on earth is AspectJ JAR file doing? How did you manage to get that error?

________
radarsh
Manikandan ThiagarajanSenior consultant

Author

Commented:
how could i set classpath to my connector jar file.

explain simple


Manikandan ThiagarajanSenior consultant

Author

Commented:
mysql-connector-java 5.0.0 b

this is my jar file name

is it correct

Commented:
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
Manikandan ThiagarajanSenior consultant

Author

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.

Commented:
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
Manikandan ThiagarajanSenior consultant

Author

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.

Commented:
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

Commented:
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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.