照着书上打但是还是报错,报错内容是: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 'DELECT FROM userdetails WHERE username='zhangsan'' at line 1
不明白哪里错了,DBUtil里面61行也没错呀。有没有人可以帮帮我TAT
DBUtil里的代码
import java.sql.*;
public class DBUtil {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
public Connection getConnection() throws ClassNotFoundException,SQLException,InstantiationException,IllegalAccessException{
String driver = config.getValue("driver");
String url = config.getValue("url");
String user = config.getValue("user");
String pwd = config.getValue("pwd");
Class.forName(driver);
con = DriverManager.getConnection(url,user,pwd);
return con;
}
public void closeAll() {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pstmt!=null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con!=null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public ResultSet executeQuery(String preparedSql,String[] param) {
try {
pstmt = con.prepareStatement(preparedSql);
if(param!=null) {
for(int i =0;i<param.length;i++) {
pstmt.setString(i+1, param[i]);
}
}
rs = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public int executeUpdate(String preparedSql,String[] param) {
int n = 0;
try {
pstmt = con.prepareStatement(preparedSql);
if(param!=null) {
for(int i =0;i<param.length;i++) {
pstmt.setString(i+1, param[i]);
}
}
n = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return n;
}
}
DBDemo的代码
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBDemo {
public static void main(String[] args) {
String selectSql = "SELECT *FROM userdetails";
String insertSql = "INSERT INTO userdetails VALUES(?,?,?,?)";
String updateSql = "UPDATE userdetails SET password=? WHERE username=?";
String delectSql = "DELECT FROM userdetails WHERE username=?";
DBUtil db = new DBUtil();
try {
db.getConnection();
ResultSet rs = db.executeQuery(selectSql, null);
System.out.println("----------原来的数据----------");
while(rs.next()) {
System.out.println("行" + rs.getRow() + ":" + rs.getInt(1) + "\t" + rs.getString(2) + "\t" + rs.getString(3) + "\t" + (rs.getInt(4) == 1 ?"男":"女"));
}
System.out.println("-----------------------------");
int count = db.executeUpdate(insertSql, new String[] {"10","李小明","abc123","1"});
System.out.println("增加了" + count + "行");
count = db.executeUpdate(updateSql, new String[] {"beijing","张三"});
System.out.println("修改了" + count + "行");
count = db.executeUpdate(delectSql, new String[] {"zhangsan"});
System.out.println("删除了" + count + "行");
rs = db.executeQuery(selectSql, null);
System.out.println("----------更新后的数据----------");
while(rs.next()) {
System.out.println("行" + rs.getRow() + ":" + rs.getInt(1) + "\t" + rs.getString(2) + "\t" + rs.getString(3) + "\t" + (rs.getInt(4) == 1 ?"男":"女"));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}