Creating a java bean to execute an sql query.
I have an sql database and i'm trying to link to it using a java bean with some sql. I'm having trouble at the moment, i thought i had cracked it with my code beolw, but i'm unable to compile the code. I get the following error for the line that i have made bold below, error:
webtech/questionaalt.java [63:1] cannot resolve symbol
symbol : variable i
location: class webtech.questionaalt
result += "<td>" + results.getObject(i).toString()+ "</td>";
^
Can anyone help????
Code:
Code:
private Connection dbconn = null;
public questionaalt()
{
try
{
Class.forName(driver);
dbconn = DriverManager.getConnection(url, uname, dpass);
/*Create an SQL statement*/
Statement statement = dbconn.createStatement();
if (statement.execute(query))
{/*Step 3) If we have a result lets loop through*/
ResultSet results = statement.getResultSet();
ResultSetMetaData metadata = results.getMetaData();
/*Validate result. Note switch to while loop if we plan on multiple results from query*/
if(results != null)
{
/*Use results setmetadata object to determine the columns*/
int li_columns = metadata.getColumnCount();
result = "<tr>";
for(int i = 1; i <= li_columns;i++)
{
result+="<td>" + metadata.getColumnLabel(i) + "</td>";
}
result += "</tr>";
while (results.next())
{
result +="<tr>";
for (int i=1; i <= li_columns; i++);
{
result += "<td>" + results.getObject(i).toString()+ "</td>";
}
result += "</tr>";
}
}
}
}
Re: Creating a java bean to execute an sql query.
for (int i=1; i <= li_columns; i++);
Re: Creating a java bean to execute an sql query.
Quote:
Originally Posted by Dilenger4
for (int i=1; i <= li_columns; i++);
Hey??
Isn't that what i already have?
Re: Creating a java bean to execute an sql query.
You are delcaring the variable i within the scope of the if() statement. The compiler sees the ; after the if() statement and treats it as one line. The variable i is within a totally different scope or out of the scope where is was declared.
Re: Creating a java bean to execute an sql query.
Quote:
Originally Posted by Dilenger4
You are delcaring the variable i within the scope of the if() statement. The compiler sees the ; after the if() statement and treats it as one line. The variable i is within a totally different scope or out of the scope where is was declared.
So how can i resolve it?
Re: Creating a java bean to execute an sql query.
Quote:
Posted by stu_fb
So how can i resolve it?
Replace the ; with {
Re: Creating a java bean to execute an sql query.
am i being stupid??....Where am i puttin the { to relpace the ; ?????? i'll then need a furhter } surely?
Re: Creating a java bean to execute an sql query.
Try this.
Code:
private Connection dbconn = null;
public questionaalt(){
try{
Class.forName(driver);
dbconn = DriverManager.getConnection(url, uname, dpass);
/*Create an SQL statement*/
Statement statement = dbconn.createStatement();
if (statement.execute(query)){
ResultSet results = statement.getResultSet();
ResultSetMetaData metadata = results.getMetaData();
/*Validate result. Note switch to while loop if we plan on multiple results from query*/
if(results != null){
/*Use results setmetadata object to determine the columns*/
int li_columns = metadata.getColumnCount();
result = "<tr>";
for(int i = 1; i <= li_columns;i++){
result+="<td>" + metadata.getColumnLabel(i) + "</td>";
}
result += "</tr>";
while (results.next()){
result +="<tr>";
for (int i=1; i <= li_columns; i++){
result += "<td>" + results.getObject(i).toString()+ "</td>";
}
result += "</tr>";
}
}
}
}
}