0
    <%@ page import = "java.sql.*" %>
<!-- Variables Declaration -->
<%! 
String connectionURL = "jdbc:mysql://localhost:3306/Tamir";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
%>

<!-- Connecting to database "Tamir" -->
<%!
void ConnectDb(){
    try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        connection = DriverManager.getConnection(connectionURL,"root","tamir");
        statement = connection.createStatement();
    } catch (Exception ex){
        System.out.print("Error in connecting");
    }
}
%>
<!-- Html Part Starts Here -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1255">
<title> Test Database </title>
</head>
<body>

<h1> test hello </h1>
<%
ConnectDb();
rs = statement.executeQuery("SELECT * FROM users");
out.println(rs.getString("nickname"));
rs.close();
%>

</body>
</html>

Error recieved: error and yes, I have the mysql connector in lib of web-inf.

Yes I have a database with the username Tamir and password tamir, and the table Users;

yes, i have tomcat installed and running.

I've been looking in the internet for ages, help would be appriciated. Cheers.

Edit: full error is as the following:

HTTP Status 500 - An exception occurred processing JSP page /select.jsp at line 34


type Exception report

message An exception occurred processing JSP page /select.jsp at line 34

description The server encountered an internal error that prevented it from fulfilling this request.

exception 
org.apache.jasper.JasperException: An exception occurred processing JSP page /select.jsp at line 34

31: <%
32: ConnectDb();
33: rs = statement.executeQuery("SELECT * FROM users");
34: out.println(rs.getString("nickname"));
35: rs.close();
36: %>
37: 


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)



root cause 
javax.servlet.ServletException: java.sql.SQLException: Before start of result set
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
    org.apache.jsp.select_jsp._jspService(select_jsp.java:110)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)



root cause 
java.sql.SQLException: Before start of result set
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
    com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:790)
    com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5212)
    com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5135)
    com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5174)
    org.apache.jsp.select_jsp._jspService(select_jsp.java:98)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)



note The full stack trace of the root cause is available in the Apache Tomcat/7.0.47 logs.


Apache Tomcat/7.0.47
    enter code here
Taymeer
  • 1
  • 1
  • 1
    Please add the stacktrace, not knowing what the exception is makes impossible to help you. A tip: where's the while (rs.next()) ? – BigMike Oct 15 '17 at 16:49
  • According to the book I've got and my understanding there was no need for a while? – Taymeer Oct 15 '17 at 16:55
  • 2
    Are you learning to write scriptlet code in a JSP from a book? Please burn that book right away. NO ONE should be putting scriptlet code in JSPs. – duffymo Oct 15 '17 at 17:16
  • @duffymo well you see my school is making me create a website with mysql and jsp as my final project but my teacher has no idea on how to teach so this book is my only resort :/ – Taymeer Oct 15 '17 at 17:52
  • 1
    @Taymeer you need to loop on rs.next(), what if the query returns no tuple ? and +1 on duffymo consideration, that ain't the proper way to use JSP, please check this answer https://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files/3180202#3180202 – BigMike Oct 15 '17 at 18:05

0 Answers0