zoukankan      html  css  js  c++  java
  • 完整使用JDBC访问数据库

    Connection con = null;
    PreparedStatement statement = null;
    ResultSet res = null;

    List<Student> students = new ArrayList<Student>();

    List<Student> getAll() {

    try {
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
                statement = (PreparedStatement) con.createStatement();
                res = statement.executeQuery("SELECT * FROM students");
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                while(res.next()){
                    String name = res.getString(1);
                    int age = res.getInt(2);
    Student student = new Student(name,age); students.add(student); } }
    catch (SQLException e1) { e1.printStackTrace(); } if (res != null) { try { res.close(); } catch (SQLException e) { e.printStackTrace(); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } return students; }

  • 相关阅读:
    solr初步了解
    jieba初步了解
    情感计算emokit
    常用命令
    java跨域访问问题记录
    mysql 免安装版配置
    Android 图片缩放方法
    spring android http
    sql join 与where的区别
    android activitygroup 使用
  • 原文地址:https://www.cnblogs.com/20158424-hxlz/p/6866883.html
Copyright © 2011-2022 走看看