为什么在执行准备好的语句时会出现java.sql.SQLSyntaxErrorException?
我的数据库类文件:为什么在执行准备好的语句时会出现java.sql.SQLSyntaxErrorException?,java,mysql,Java,Mysql,我的数据库类文件: package Model; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.sql.Connection; import java.s
package Model;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class Database {
private LinkedList<Person> people;
public Database() {
people = new LinkedList<Person>();
}
private Connection con;
public void connect() throws Exception{
if(con != null) return;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new Exception("Driver not found");
}
String Url = "jdbc:mysql://localhost:3306/jdbc";
Connection con;
con = DriverManager.getConnection(Url, "root", "089abc2019");
System.out.println("Connected: " + con);
}
public void disconnect() {
if(con != null) {
try {
con.close();
} catch (SQLException e) {
System.out.println("Can't close connection");
}
}
}
public void save() throws SQLException {
String checkSql = "Select count * from jdbc where id=?";
String Url = "jdbc:mysql://localhost:3306/jdbc";
Connection con = DriverManager.getConnection(Url, "root", "089abc2019");
if (con==null) {
fail("Connection couldn't be established");
}
PreparedStatement checkStmt = con.prepareStatement(checkSql);
for (Person person: people) {
int id = person.getId();
checkStmt.setInt(1, id);
ResultSet checkResult = checkStmt.executeQuery();
checkResult.next();
int count = checkResult.getInt(1);
System.out.println("Count for person wiht ID: " + id + "is " + count);
}
checkStmt.close();
}
private void fail(String string) {
System.out.println(string);
}
public void addPerson(Person person) {
people.add(person);
}
public void removePerson(int index) {
people.remove();
}
public List<Person> getPeople(){
return Collections.unmodifiableList(people);
}
public void saveToFile(File file) throws IOException{
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
Person[] persons = people.toArray(new Person[people.size()]);
oos.writeObject(persons);
oos.close();
}
public void loadFromFile(File file) throws IOException{
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
try {
Person[] person = (Person[]) ois.readObject();
people.clear();
people.addAll(Arrays.asList(person));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ois.close();
}
}
控制台中的结果:
Running database test
Connected: com.mysql.cj.jdbc.ConnectionImpl@316bcf94
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from jdbc where id=0' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1003)
at Model.Database.save(Database.java:74)
at TestDatabase.main(TestDatabase.java:26)
您的sql语法不正确<代码>计数是一个错误。这个 应该是
Select count(*) from jdbc where id=?
这回答了这个问题,但我将这个问题标记为“它是由打字错误或无法再复制的问题引起的”,因为,实际上,查找一分钟的通用“sql计数”表明,大多数DBMS需要在计数函数中使用括号。Amd有点可笑,一个“您的sql语法不正确。”与“SQLSyntaxErrorException”相反的语句只是重复了它本身。这不是重点,即使我更改了,错误仍然会发生@RedRabbit发生了相同的错误还是有点不同?发生了相同的错误,我不确定数据库是否存在一些问题
Select count * from jdbc where id=?
Select count(*) from jdbc where id=?