java使用mysql-connector-java查询数据库

    //查询
    public static void query() {
        try {
            //调用Class.forName()方法加载驱动程序
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.10.102/?useUnicode=true&characterEncoding=UTF-8", "", "");//url 账号 密码
            Statement stmt = conn.createStatement(); //创建Statement对象
            ResultSet rs = stmt.executeQuery("select * from sql.t_backend_user_role where id=1");//创建数据对象
            while (rs.next()) {
                int id = rs.getInt("id");
                String create_time = rs.getString("create_time");
                System.out.println(id);
                System.out.println(create_time);
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

你可能感兴趣的:(程序开发)