m0_73919856 2023-04-14 21:21 采纳率: 100%
浏览 24
已结题

关于#Java问题 :mysql数据库访问优化

照着书上打但是还是报错,报错内容是: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

img

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();
        }

    }

}


  • 写回答

2条回答 默认 最新

  • Huazie 全栈领域优质创作者 2023-04-14 21:59
    关注

    删除语句有问题,是 DELETE FROM 不是 DELECT FROM

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月25日
  • 已采纳回答 4月17日
  • 创建了问题 4月14日

悬赏问题

  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃