我想在SQL语法中使用java变量。请建议是否可能。这是我的代码
String Userid_input=request.getParameter("Userid");//I am getting dynamic input from User
String Password_input=request.getParameter("Password");
下面给出了数据库连接
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Proj_name","root","root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select User_type from Signup where User_name.User_name=Userid_input");//**Userid_input** is a java variable
我收到以下错误,
发生异常com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:'where子句'中的未知列'Userid_input'
答案 0 :(得分:0)
试试这个:
ResultSet rs=st.executeQuery("select User_type from Signup where User_name=" + Userid_input);
答案 1 :(得分:0)
答案 2 :(得分:0)
或者,您可以使用预准备语句:
字符串查询=" SELECT User_type FROM注册WHERE User_name.User_name =?&#34 ;;
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1,Userid_input);
ResultSet rs = ps.executeQuery();
有关准备好的陈述的更多信息http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html