Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JSP会话的SQL语句_Sql_Jsp_Session - Fatal编程技术网

使用JSP会话的SQL语句

使用JSP会话的SQL语句,sql,jsp,session,Sql,Jsp,Session,我试图在sql语句中使用我在JSP中创建的会话。但是有困难 我已经捕获了它,这是我做的SQL语句,但不起作用 String mysql="select * from table1 where cottCode="<%= session.getAttribute( "theName" ) %>""; 感谢您的帮助。我想您正在尝试这样做: <% String name = session.getAttribute( "theName" ); String mysq

我试图在sql语句中使用我在JSP中创建的会话。但是有困难

我已经捕获了它,这是我做的SQL语句,但不起作用

String mysql="select * from table1 where cottCode="<%= session.getAttribute( "theName" ) %>"";

感谢您的帮助。

我想您正在尝试这样做:

<%
    String name = session.getAttribute( "theName" );
    String mysql = "select * from table1 where cottCode=" + name;
    // then connecting MySql server using driver in JSP to execute the SQL query
%>
这种代码称为jspscriptlet

然而,表达方式如下:

<%= session.getAttribute( "theName" ) %>
它们被称为JSP表达式。它们用于将脚本语言表达式的值(转换为字符串)插入到返回给客户端的数据流中。像这样:

<html>
    <body>
        <p>hi, my name is <%= session.getAttribute( "theName" ) %>
        </p>
    </body>
</html>
所以我认为你不能在脚本的中间使用这个表达式。以下是相关主题的教程,希望对您有所帮助:

谢谢您的回复,不过我找到了另一个解决方案。我使用它将其放入变量字符串pbutton=request.getParameterT2;那么对于声明。。。。字符串mysql=从表1中选择*,其中cottCode='+pbutton+';完成。谢谢。@Merked您真的应该使用准备好的语句来避免SQL注入,特别是当您使用请求参数P2时-
Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/<db name>","<username>","<pwd>");

Statement s = c.createStatement();
String sql = "SELECT * FROM table where  cottCode = '"+session.getAttribute("<your session asign attribute>")+"'";
resultSet= s.executeQuery(sql);

if(resultSet.next()){

<!-- rendering part -->

}