Hi all, i'm newbie with jsp and i'm trying to connect with mysql through a select with a variable passed from an another page...
The code i have is what follows:

<%
// Prendo la variabile dal form
String username=request.getParameter("username");
String sql="SELECT * FROM cmascella_users WHERE username = " + username + " ";
String connectionURL = "jdbc:mysql://localhost/NAMETABLE?user=NAMEUSER&password=PASS";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
//out.write(sql);
%>

<html><body>

<%

Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "NAMEUSER", "PASS");
statement = connection.createStatement();
rs = statement.executeQuery(sql);

while (rs.next()) {
out.println(rs.getString("username")+"<br>");
}

rs.close();

%>

The problem is that i get back an error ...this one:

javax.servlet.ServletException: Unknown column 'Trottolone' in 'where clause'

...it's seem the sql is trying to look for a column named "Trottolone" that is the username!!! What wrong?! :-(

Thanks

The solution is do not do database connectivity from inside a JSP. Check this thread to see how to go about this the right way.

Wow.. and where please? Is somewhere some examples, some tutorials... something please?

Here you go.

Thanks A lot for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.