在 Mysql where 子句中使用 java 变量
Using java variables in Mysql where clause
我想在 SQL 中使用 java 变量 syntax.Please 建议是否可能 not.Here 是我的代码
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 clause'
中的未知列 'Userid_input'
试试这个:
ResultSet rs=st.executeQuery("select User_type from Signup where User_name=" + 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
或者,您可以使用准备好的语句:
String query = "SELECT User_type FROM Signup WHERE User_name.User_name = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1, Userid_input);
ResultSet rs = ps.executeQuery();
有关准备好的语句的更多信息http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
我想在 SQL 中使用 java 变量 syntax.Please 建议是否可能 not.Here 是我的代码
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 clause'
中的未知列 'Userid_input'试试这个:
ResultSet rs=st.executeQuery("select User_type from Signup where User_name=" + 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
或者,您可以使用准备好的语句:
String query = "SELECT User_type FROM Signup WHERE User_name.User_name = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1, Userid_input);
ResultSet rs = ps.executeQuery();
有关准备好的语句的更多信息http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html