Exception error.
[nobr]Hi, I have a class java to try to obtain some information.
I obtain a String with the users' names.
First I try to count how many rows I have obtained, and later I create a String of this length and I try to fill it whit the information (users' names)
My code is:
package connectDB;
import java.lang.*;
import java.sql.*;
publicclass MyConnection{
Connection dbconn;
ResultSet results;
PreparedStatement sql;
public MyConnection(){
try
{
Class.forName("org.gjt.mm.mysql.Driver");
try{
dbconn = DriverManager.getConnection("jdbc:mysql://localhost/paleodbTemp?user=XXX&password=YYY");
}catch (SQLException s)
{
System.out.println("SQL Error<br>");
}
}catch (ClassNotFoundException err)
{
System.out.println("Class loading error");
}
}
....
....
public String[] getNameUserWithFiles(String log){
int numberRows=0;
String []StringData =null;
try{
String consulta ="SELECT login_user from File_user where login_user != '"+log+"' group by login_user";
sql = dbconn.prepareStatement(consulta);
results = sql.executeQuery();
while(results.next()){
numberRows++;
}
results.first();
StringData =new String[numberRows];
int i=0;
while(results.next()){
StringData[i] = results.getString("login_user");
i++;
}
}catch (SQLException s)
{
System.out.println("SQL Error<br>");
}
StringData;
}
I obtain the follow exception error:
javax.servlet.ServletException: org.gjt.mm.mysql.ResultSet.first()Z
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.admin.struct.users_jsp._jspService(users_jsp.java:199)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
causa ra韟
java.lang.AbstractMethodError: org.gjt.mm.mysql.ResultSet.first()Z
connectDB.MyConnection.getNameUserWithFiles(MyConnection.java:108)
org.apache.jsp.admin.struct.users_jsp._jspService(users_jsp.java:142)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
How it can be the error?
The SQL-question makes it correctly
Thanks[/nobr]
does this method even compile?
actually, i think this whole method is a bad idea.
it's a bad idea to put HTML in persistence classes. what if you change the page? or go with another UI?
I'd write it more like this:
package cruft;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* Another Forum solution
* @author duffymo
* Date: Nov 20, 2006
* Time: 8:12:15 AM
*/
public class TossThis
{
public String[] getNameUserWithFiles(Connection dbconn, String log)
{
if ((log == null) || ("".equals(log.trim())))
throw new IllegalArgumentException("log file name cannot be blank or null");
List<String> stringData = new ArrayList<String>();
PreparedStatement ps = null;
ResultSet results = null;
try
{
String consulta = "SELECT login_user from File_user where login_user != ? order by login_user";
ps = dbconn.prepareStatement(consulta);
ps.setString(1, log);
results = ps.executeQuery();
while (results.next())
{
stringData.add(results.getString("login_user"));
}
}
catch (SQLException s)
{
s.printStackTrace();
}
finally
{
try { if (results != null) results.close(); } catch (Exception e) { e.printStackTrace(); }
try { if (ps != null) ps.close(); } catch (Exception e) { e.printStackTrace(); }
}
return stringData.toArray(new String[0]);
}
}
%
[nobr]Thanks everybody. I have solved this doing:
public String[] getNameUserWithFiles(String log){
int numberRows=0;
ResultSet rs = null;
String []StringData = null;
try{
String consulta = "SELECT login_user from File_user where login_user != '"+log+"' group by login_user";
sql = dbconn.prepareStatement(consulta);
results = sql.executeQuery();
while(results.next()){
numberRows++;
}
StringData = new String[numberRows];
rs = sql.executeQuery();
int i=0;
while(rs.next()){
StringData[i] = rs.getString("login_user");
i++;
}
}catch (SQLException s)
{
System.out.println("SQL Error<br>");
}
return StringData;
}
[/nobr]
Hi duffymo, I have try do this:
public String[] getNameUserWithFiles(String log){
//int numberRows=0;
//ResultSet rs = null;
//String []StringData = null;
List<String> stringData = new ArrayList<String>();
try{
String consulta = "SELECT login_user from File_user where login_user != '"+log+"' group by login_user";
sql = dbconn.prepareStatement(consulta);
results = sql.executeQuery();
//rs = results;
while(results.next()){
stringData.add(results.getString("login_user"));
//numberRows++;
}
}catch (SQLException s)
{
s.printStackTrace();
}
finally
{
try {
if (results != null) results.close();
} catch (Exception e) {
e.printStackTrace();
}
/*try {
if (ps != null) ps.close();
} catch (Exception e) {
e.printStackTrace();
}*/
}
return stringData.toArray(new String[0]);
}
but I think (I have looked for information) that (generic types)
List<String> stringData = new ArrayList<String>();
is only possible if you have installed Java 1.5, Isn磘 it?
In my case I have installed java 1.4 (j2sdk1.4.2_10), and will not be able to do this, no?
thanks anyway