屠一乐 2022-04-21 11:41 采纳率: 25%
浏览 73
已结题

jdbc连不上mysql 报错为 Access denied for user 'root'@'***' (using password: YES)



import small_program.linksql.Student;
import small_program.linksql.utils.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;

public class StudentDaoimpl implements StudentDao {
    @Override
    public ArrayList<Student> findAll()  {
        ArrayList<Student> students = new ArrayList<>();
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://10.200.36.189:3306/db14","root","123456");
            //注册,连接
            //获取执行对象
//            connection = JDBCUtils.getConnection();
            statement = connection.createStatement();
            //执行SQL并接收返回结果集
            String sql = "select * from student";

            rs = statement.executeQuery(sql);
            //处理结果
            while (rs.next()) {
                Integer sid = rs.getInt("sid");
                String name = rs.getString("name");
                Integer age = rs.getInt("age");
                Date date = rs.getDate("date");

                Student student = new Student(sid, name, age, date);
                students.add(student);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(rs,statement,connection);
        }
    return students;
    }

    @Override
    public Student findById(Integer id) {
        Student student1 = new Student();
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        try {
            //注册,连接
            //获取执行对象
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://10.200.36.189:3306/db14","root","123456");
//            connection = JDBCUtils.getConnection();
            statement = connection.createStatement();
            //执行SQL并接收返回结果集
            String sql = "select * from student where sid = id";

            rs = statement.executeQuery(sql);
            //处理结果
            while (rs.next()) {
                Integer sid = rs.getInt("sid");
                String name = rs.getString("name");
                Integer age = rs.getInt("age");
                Date date = rs.getDate("date");
                student1.setSid(sid);
                student1.setName(name);
                student1.setAge(age);
                student1.setBirthday(date);

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(statement,connection);
            return student1;
        }
    }
    @Override
    public int insert(Student student) {
        return 0;
    }

    @Override
    public int update(Student student) {
        return 0;
    }

    @Override
    public int del(Integer id) {
        return 0;
    }
}

  • 写回答

4条回答

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 已结题 4月21日
      • 已采纳回答 4月21日
      • 创建了问题 4月21日

      悬赏问题

      • ¥15 C# 循环读写数据中途突然变慢
      • ¥100 CVI2012 做暗通道图像去雾思路详细步骤
      • ¥15 用Java实现双端队列
      • ¥150 ID3决策树实现分类
      • ¥15 multisim10安装后,找不到NI License Manager的程序来安装许可证
      • ¥15 C++银行卡系统 Help!
      • ¥15 R语言数据分析的相关问题
      • ¥15 模型导入SP后贴图纹理只有一个,拆了四张UV的,怎么解决?
      • ¥15 检索带order by 非常慢
      • ¥20 python 爬虫 token 加密方式