Home arrow static arrow Java Programming [Archive] - simple problem from a school student
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - simple problem from a school student
This topic has 4 replies on 1 page.

Posts:41
Registered: 8/8/04
simple problem from a school student  
Aug 8, 2004 4:57 AM



 
hi,i have a small problem with writing a program
i have a method in a java class which creates a connection to a sql database of mine.and reads the values from the database into variables in java,but the problem is i want to call up this method from another class and use the variables in the other class and do a TextArea.setText,but the other problem is when i do this it only saves the last entry name from the database in TextArea.

i had a couple of ideas to add the values into a array or a vector.and add it that way,but i was unsuccessful.
here is the method

static String NameArray [] = new String [100];
static String SurnameArray [] = new String [100];
static String UsernameArray [] = new String [100];
static String PasswordArray [] = new String [100];
static String GroupArray [] = new String [100];
public static void UserLinkList ()
{
try
{
InputStream in = null;
String url = "jdbc:odbc:Users";
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
Connection dbconn = DriverManager.getConnection (url);
Statement stmt = dbconn.createStatement ();
ResultSet rs = stmt.executeQuery ("SELECT Name, Surname, Username, Password, Group FROM Userlist;");

int i=-1;
while (rs.next ())
{
i++;
name= rs.getString (1);
surname = rs.getString (2);
username = rs.getString (3);
password = rs.getString (4);
group = rs.getString (5);
//Adding values from database to specified arrays
NameArray = name;
SurnameArray = surname;
UsernameArray = username;
PasswordArray = password;
GroupArray = group;
//System.out.println (name + " \t " + surname + " \t " + username + "\t\t " + password);
//System.out.println();
//System.out.println (name);

//System.out.println(userarray);

}

rs.close ();
stmt.close ();
dbconn.close ();

}

plz help me on howto use the method in another class and do a Textarea.setText successfully with the different variables
 

Posts:18,384
Registered: 21.03.00
Re: simple problem from a school student  
Aug 8, 2004 5:03 AM (reply 1 of 4)



 
Hi,

Change the method so it returns a String, and append all values in the loop to a StringBuffer:

In the loop
  buffer.append(rs.getString(1) + "\t");  buffer.append(rs.getString(2) + "\t");  buffer.append(rs.getString(3) + "\t");  buffer.append(rs.getString(4) + "\t");  buffer.append(rs.getString(5) + "\n");


And after the loop

  return buffer.toString();  //After you have closed the resources


/Kaj
 

Posts:41
Registered: 8/8/04
Re: simple problem from a school student  
Aug 8, 2004 5:26 AM (reply 2 of 4)



 
i get an error when i compile this: Error: A return statement with expression must be contained in a method declaration that is declared to return a value.

public static void UserLinkList ()
{
try
{
InputStream in = null;
String url = "jdbc:odbc:Users";
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
Connection dbconn = DriverManager.getConnection (url);
Statement stmt = dbconn.createStatement ();
ResultSet rs = stmt.executeQuery ("SELECT Name, Surname, Username, Password, Group FROM Userlist;");
StringBuffer buffer = new StringBuffer ();

while (rs.next ())
{
buffer.append (rs.getString (1) + "\t");
buffer.append (rs.getString (2) + "\t");
buffer.append (rs.getString (3) + "\t");
buffer.append (rs.getString (4) + "\t");
buffer.append (rs.getString (5) + "\n");
return buffer.toString ();
//Adding values from database to specified arrays

//System.out.println (name + " \t " + surname + " \t " + username + "\t\t " + password);
//System.out.println();
//System.out.println (name);

//System.out.println(userarray);

}

rs.close ();
stmt.close ();
dbconn.close ();

}
 

Posts:1,135
Registered: 1/16/04
Re: simple problem from a school student  
Aug 8, 2004 6:00 AM (reply 3 of 4)



 
Question answered in this thread...
http://forum.java.sun.com/thread.jsp?forum=31&thread=544845&tstart=0&trange=15

Here it is again...

Well here is your method declaration...
public static void UserLinkList ()

notice that you declared it as a void meaning that this method does not return a value of any type...
but... here...
return buffer.toString ();

you are returning a String...
and that is why you are getting the error...

If you want to return a String then the method must be declared to return a String...
public static String UserLinkList ()

As you can note, now it has a return type of String and no longer void...

Hope this helps...

- MaxxDmg...
 

Posts:826
Registered: 7/1/03
Re: simple problem from a school student  
Aug 8, 2004 6:04 AM (reply 4 of 4)



 
Hi,

Change your method signature:
public static void UserLinkList () {
to
public static String UserLinkList () { 


Don't return until the end of the method or you'll skip all your statements where you nicely closed your resources.

I'm assuming you do have a catch statement in there too? If you don't catch your exceptions you need to declare that the method throws them!

/k1
 
This topic has 4 replies on 1 page.