Reputation: 3
I had some problem with the variable COMICI, as I need it in two different state (connected to the variable username) I put it in and outside of if
, but when I call it eclipse told me that it cannot be considerate a variable.
Any idea about why?
String username = request.getParameter("username");
if(username!= null){
String dbURL = "jdbc:mysql://localhost:3306/login";
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "");
Statement st1 = con.createStatement();
ResultSet rs1;
st1.executeQuery("SELECT 'COMICI' from categorie WHERE UTENTE_ID = '"+username+"' ");
PreparedStatement ps=(PreparedStatement)con.prepareStatement("SELECT COMICI from categorie WHERE UTENTE_ID = '"+username+"' ");
ResultSet rs=ps.executeQuery();
rs.next();
String COMICI=rs.getString("COMICI");
} else {
String COMICI=("null");
}
%>
<%
if(username!=null) {
%>
<%
//select * from categorie where UTENTE_ID='
out.print("Hello, "+username+" Welcome to Profile");
out.print("Hello,"+ COMICI +" Welcome to Profile");
}
%>
Upvotes: 0
Views: 68
Reputation: 59986
You have many problems in your code :
First
You have to declare your variables outside your if or else block :
String COMICI = "";
if (username != null) {
COMICI = rs.getString("COMICI");
...
}else{
COMICI = null;
}
Second
To inisialize your variable you don't need to make parentheses:
COMICI = ("null");
Instead use :
COMICI = "null";
Third
Instead of using Statement this can cause syntax error or SQL Injection, you have to use PreparedStatement its more secure and more helpful.
Fourth
To get result from your ResultSet you have to use :
if(rs.next()){
COMICI = rs.getString("COMICI");
}
Upvotes: 1
Reputation: 681
This works,try it
String username = request.getParameter("username");
string COMICI=null;
if(username!= null){
String dbURL = "jdbc:mysql://localhost:3306/login";
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "");
Statement st1 = con.createStatement();
ResultSet rs1;
st1.executeQuery("SELECT 'COMICI' from categorie WHERE UTENTE_ID = '"+username+"' ");
PreparedStatement ps=(PreparedStatement)con.prepareStatement("SELECT COMICI from categorie WHERE UTENTE_ID = '"+username+"' ");
ResultSet rs=ps.executeQuery();
if( rs.next())
{
COMICI=rs.getString("COMICI");
} else {
COMICI="null";
}
%>
<%
if(username!=null) {
%>
<%
//select * from categorie where UTENTE_ID='
out.print("Hello, "+username+" Welcome to Profile");
out.print("Hello,"+ COMICI +" Welcome to Profile");
}
%>
Upvotes: 0