connecting jsp and mysql with tomcat

8 views
Skip to first unread message

Vipul Paralikar

unread,
Apr 2, 2010, 5:41:44 AM4/2/10
to open-traini...@googlegroups.com

Hi, i am trying to connect to mysql database thru JSP. I have  Suse Linux 11.2. 


here is my code -- 


<%@ page import="java.sql.*" %> 
<% 
String connectionURL = "jdbc:mysql://localhost:3306/songdb?username="root"&password="root""; 
Connection connection = null; 
Statement statement = null; 
ResultSet rs = null; 
%> 
<html><body> 

<% 
Class.forName("com.mysql.jdbc.Driver").newInstance(); 
connection = DriverManager.getConnection(connectionURL, "", ""); 
statement = connection.createStatement(); 
rs = statement.executeQuery("SELECT * FROM songs"); 

while (rs.next()) { 
out.println(rs.getString("SongId")+"<br>"); 


rs.close(); 
%> 

</body></html> 


but when i try to run this thru browser by giving http://localhost:8080/jsp-examples/test.jsp it gives the following error - 


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: 2 in the jsp file: /test.jsp 
Generated servlet error: 
Syntax error on tokens, delete these tokens 


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:397) 
org.apache.jasper.compiler.Compiler.compile(Compiler.java:288) 
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267) 
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255) 
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556) 
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:296) 
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295) 
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245) 
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.7 logs. 


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

Apache Tomcat/6.0.20



could any one tell me whats wrong here? 

thanks, 

Vipul Paralikar

Jigar Amin

unread,
Apr 3, 2010, 1:26:57 AM4/3/10
to open-training-community
Hi Vipul,

Try this

Keep Your Connection String Object as follow

String connectionURL = "jdbc:mysql://localhost:3306/
songdb"

than create two Variables for UserName and Password

String uname="root";
String passwd="root"

and finally while creating connection object keep parameters as
follows

connection = DriverManager.getConnection(connectionURL,uname,
passwd);


else keep code as it is.

Best of Luck

Reply all
Reply to author
Forward
0 new messages