java jdbc为sql查询抛出错误,该查询要求仅返回特定行的详细信息

kokeuurv  于 2021-06-19  发布在  Mysql
关注(0)|答案(2)|浏览(263)

我有一个名为“飞机”的数据库,里面有一个名为booking的表。预订表有phone(int type)、name(text type)、address(text type)、city(text type)、destination(text type)、date(text type)列。我只想获取phone列的值或数据等于用户输入的phone number的行。我为此编写了以下代码。对于上下文,我使用java和jdbc

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/airplane";
static final String USER = "root";
static final String PASS = "";

Connection conn = null;

try
  {
    System.out.println("enter cell no");
    int cell=sc.nextInt();
    conn = DriverManager.getConnection(DB_URL, USER, PASS);
    System.out.println("Connected database successfully...");

   // My SQL SELECT query.

    String query = "SELECT * FROM `booking` where phone=cell";

   // creating the java statement
   Statement st = conn.createStatement();

   /**executing the query, and getting a java resultset of all rows whose phone
    column has the value equal to one entered by user and stored by me in 
  int variable cell**/

   ResultSet rs = st.executeQuery(query);

         //iterate through the java resultset
         while (rs.next())
           {
            int Phone = rs.getInt("phone");
            String Name = rs.getString("name");
            String Address = rs.getString("address");
            String City = rs.getString("city");
            String Destination = rs.getString("destination");
            String Date = rs.getString("date");

// print the results
           System.out.format("%d, %s, %s, %s, %s, %s\n", Phone, Name, Address, City, Destination, Date);
                       }

                        st.close();
                    }
                    catch (Exception e)
                    {
                        System.err.println("Got an exception! ");
                        System.err.println(e.getMessage());
                    }

我得到一个错误:

Got an exception! 
Unknown column 'cell' in 'where clause'

我做错什么了?

kyks70gy

kyks70gy1#

你会看到它要跑了
string query=“选择*自 booking 其中phone=“+”\“%s\”;
query=string.format(查询,单元格);

gxwragnw

gxwragnw2#

试试这个:

String query = "SELECT * FROM `booking` where phone=" + cell;

更好的处理方法是使用preparedstatement
在这种情况下,查询如下所示:

String query = "SELECT * FROM `booking` where phone=?"

以及

statement.setInt(1, cell);

相关问题