当前位置: 首页 > 知识库问答 >
问题:

如何解决com.mysql.jdbc.exceptions.mysqlSyntaxerrorexception:表“task4db.tst”不存在异常

冯哲彦
2023-03-14
public static void search() throws ClassNotFoundException, SQLException

{    
     Class.forName("com.mysql.jdbc.Driver");

    Connection con = DriverManager.getConnection("jdbc:mysql://localhost/task4DB?"
              + "user=root&password=123");

   PreparedStatement st = con.prepareStatement("select * from tst");

    ArrayList<String> fName = new ArrayList<String>();
    ArrayList<String> lName = new ArrayList<String>();
    ArrayList<Integer> ids = new ArrayList<Integer>();


    ResultSet RS=st.executeQuery("select * from tst ");

      loop1:
    for(String s : fName )

    {

          while (RS.next()) {
fName.add(RS.getString("fName"));


    }

      for(int i=0;i<fName.size();i++ )

      {
          System.out.println(fName.get(i));

      }

}

共有1个答案

昌乐
2023-03-14

createStatement不返回preparedstatment-它返回一个语句。您需要将SQL传递给PrepareStatement方法:

PreparedStatement st = con.prepareStatement("select * from tst");

然后:

ResultSet rs = st.executeQuery();

虽然您仍然可以调用executequery的重载(它采用SQL),但通常不应该调用。(我认为让PreparedStatement扩展Statement可能是一个错误。)

while (rs.next()) {
    fName.add(rs.getString("fName"));
}
 类似资料: