我想在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"
试试这个,
String query = "select User_type from Signup where User_name.User_name="+Userid_input;
ResultSet rs = st.executeQuery(query);
参考:https://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html
试试这个:
ResultSet rs=st.executeQuery("select User_type from Signup where User_name=" + Userid_input);
或者,您可以使用准备好的语句:
字符串查询="SELECT User_type FROM Signup WHERE User_name.User_name=?";
PreparedStatement ps=conne.prepareStatement(查询);
ps.setInt(1,Userid_input);
结果集rs=ps.executeQuery();
有关准备好的报表的更多信息http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html