3

I tested that my Java program correctly retrieve data from MySQL. However, the problem is between Java and JSP. The JSP page can't retrieve data from the java program.

Please help me guys.

It gives the output as null:

null
null
.
.
.
hai

My jsp page:

<%@ page import="com.zoo.MySQLAccess"%>
<html>
<head>
</head>
<body>
    <%
    MySQLAccess x= new MySQLAccess();
    String[] arr =x.getRows();
    out.print("1" +arr[0]);
    %>
    <% for(String str:arr) { %>
        <div style="height: 100px">
        <% out.print(str); %>
        </div>
    <% } %>
    <h1>hai</h1>
</body>
</html>

My java page is:

package com.zoo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import com.mysql.jdbc.Driver;

public class MySQLAccess {

    public String[] getRows() {
        String[] a = new String[100];
        try {
            // Class.forName("com.mysql.jdbc.Driver");
            Connection connection = DriverManager
                    .getConnection("jdbc:mysql://localhost:3306/sankar?"
                            + "user=root&password=9788129325");
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement
                    .executeQuery("SELECT * FROM sankar.datas");

            int i = 0;

            while (resultSet.next()) {
                a[i] = resultSet.getString("name");
                i++;
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return a;
    }
}
sdanzig
  • 4,510
  • 1
  • 23
  • 27
user2815407
  • 147
  • 2
  • 9
  • Are there any exceptions in the log? Note that if you returns a `List` instead of an array, you could tell the difference between failure and an empty result set... – Jon Skeet Oct 28 '13 at 15:19
  • [Avoid using scriptlets in JSP pages](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files/3180202#3180202). – GriffeyDog Oct 28 '13 at 15:36
  • There is no exceptions. the o/p is as i said it displayed as null null.... – user2815407 Oct 28 '13 at 15:43

3 Answers3

1

Since no elements are recorded, you have either one of two problems. You're not connecting to your database, or you're not querying your table correctly.

Check that you can connect to the "sankar" database on localhost, port 3306 as root with password 9788129325 using the command-line mysql client.

If that works, then check in mysql that you have a "datas" table in this sankar database. You don't really need to have "sankar.datas". You can just have "select * from datas".

If you can query the table, make sure it has a "name" column.

If it has a name column, make sure there is at least 1 row in this column.

If it has one row, make sure the "name" for that row is not null.

If this is the case, then your error is probably that you're loading your driver incorrectly. First off, you hopefully have the mysql-connector-java-<some version>-bin.jar in your application's classpath? Perhaps in your webapp's "lib" directory? Also, I assume that Class.forName that you commented out is somewhere else in the code? If not, you need that. Also, you can do it like this:

Class.forName("com.mysql.jdbc.Driver").newInstance();

The newInstance is a "workaround" for some Java implementations. Maybe yours needs that.

Anyway, again, the stack trace on your Java server is likely printing to the console, and should give you a better idea of what's going wrong.

sdanzig
  • 4,510
  • 1
  • 23
  • 27
  • I already told that when i tested the above java program with a main class java.It correctly outputs the mysql data.the problem is connection between java and jsp. – user2815407 Oct 29 '13 at 05:52
  • Are we seeing the full output? If the array is null, it should at least start with "1null". And are you getting about 100 "null" lines? – sdanzig Oct 29 '13 at 13:16
0

Probably your resultSet is empty, so in that case you are passing an array of 100 nulls.

That's where your NullPointerException comes from.

Eel Lee
  • 3,513
  • 2
  • 31
  • 49
  • But when i tested with a test java pgm. it comes o/p as in database ... the problem is retrieving from java to jsp.. – user2815407 Oct 28 '13 at 15:49
  • @user2815407 Sorry, but I don't understand. What is "java pgm" and what means " it comes o/p as in database" ? – Eel Lee Oct 28 '13 at 15:52
  • I written an main method in another java class.and run the above method..then it gives o/p as it is in mysql.. – user2815407 Oct 28 '13 at 16:16
0

You should resize your array before returning it:

return Arrays.copyOf(a, i);

You also need to move your int i outside of the try/catch. But be creating a fixed (max) size of 100 isn't a good idea...

Tyco
  • 131
  • 4
  • Without result, your arr[0] in JSP will produce a IndexOutOfBoundException! – Tyco Oct 28 '13 at 15:40
  • But when i tested with a test java pgm. it comes o/p as in database ... the problem is retrieving from java to jsp.. – user2815407 Oct 28 '13 at 15:46
  • Replace your e.printStackTrace(); by throw new RuntimeException(e);, I think you have an issue here. – Tyco Oct 28 '13 at 15:51