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;
}
}