简体   繁体   English

更新:从Java执行SQL查询时出现问题

[英]Updated: Problem with executing SQL query from Java

Updated: so as per the suggestions i changed all the column name to strings and added prepared statements- 更新:根据建议,我将所有列名都更改为字符串,并添加了准备好的语句-

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/minor","root","alphabet")) {
            Statement st = conn.createStatement();
            PreparedStatement stmt= conn.prepareStatement("select * FROM ? where name=? ;");
            PreparedStatement stmt2= conn.prepareStatement("select * FROM ? where name=? ;");
            stmt.setString(1, day_1);
            stmt.setString(2, faculty1);
            stmt2.setString(1,day_1);
            stmt2.setString(2, faculty2);
            ResultSet rs=stmt.executeQuery();
            ResultSet rs1= stmt.executeQuery();

The day and faculty is retrieved from input screen, the queries work just fine in mysql workbench but the 'select' keyword goes missing when i try to run it from Java, see the following error- 从输入屏幕中检索日期和教职人员,查询在mysql工作台中工作正常,但是当我尝试从Java运行它时,“ select”关键字丢失了,请参见以下错误- 没有选择关键字 The faculty1, faculty2 is retrieved from the following- 教职员工1,教职员工2是从以下位置检索的: 取回 The database looks like this- 数据库看起来像这样- 数据库

  1. I would recommend to use PreparedStatement instead of Statement, then at least you can bind your variables; 我建议使用PreparedStatement代替Statement,然后至少可以绑定变量。
  2. You query select from time_interval from day_selected is not correct, I don't think that it will execute anywhere, you need to have something between select and from, and not two from in one statement. select from time_interval from day_selected查询select from time_interval from day_selected是不正确的,我认为它不会在任何地方执行,您需要在select和from之间添加一些内容,而不是在一条语句中包含两个from。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM