请问通过 jdbc 精确查询 MySQL 数据库怎么写?

问答 秋意浓 ⋅ 于 2019-10-28 18:01:56 ⋅ 最后回复由 青牛 2019-10-30 08:32:00 ⋅ 1562 阅读

请问通过jdbc精确查询mysql数据库怎么写?

成为第一个点赞的人吧 :bowtie:
回复数量: 1
  • 青牛 国内首批大数据从业者,就职于金山,担任大数据团队核心研发工程师
    2019-10-30 08:32:00

    public static void main(String[] args) {
    System.out.println("MySQL JDBC Example.");
    Connection conn = null;
    String url = "jdbc:mysql://...:3306/test?autoReconnect=true&useSSL=false";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "XXXXXXXX";
    Statement stmt = null;
    ResultSet rs = null;
    try {
    Class.forName(driver);
    conn = DriverManager.getConnection(url, userName, password);
    stmt = conn.createStatement();
    String sql = "select * from AAA";
    rs = stmt.executeQuery(sql);
    while (rs.next()) {
    int id = rs.getInt("emp_id");
    String name = rs.getString("name");
    System.out.println("id = " + id + ", name = " + name);
    }
    // 关闭资源
    rs.close();
    stmt.close();
    conn.close();
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (rs != null) {
    try {
    rs.close();
    } catch (SQLException sqlEx) { } // ignore
    }
    if (stmt != null) {
    try {
    stmt.close();
    } catch (SQLException sqlEx) { } // ignore
    }
    }
    }

暂无评论~~
  • 请注意单词拼写,以及中英文排版,参考此页
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
  • 支持表情,可用Emoji的自动补全, 在输入的时候只需要 ":" 就可以自动提示了 :metal: :point_right: 表情列表 :star: :sparkles:
  • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif,教程
  • 发布框支持本地存储功能,会在内容变更时保存,「提交」按钮点击时清空
Ctrl+Enter