zoukankan      html  css  js  c++  java
  • JDBC的使用——实习日志7.11

    一、JDBC

    JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。

    二、准备工作

    将Mysql的jar包导入到项目中,并右键点击add jar as library

    三、JDBC操纵数据库基本步骤

    1.加载驱动

                try {
                    Class.forName("com.mysql.jdbc.Driver");
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

    2.创建连接

     connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/user?useSSL=true&"+
                        "characterEncoding=utf-8&user="+
                        "root&password=123");
                System.out.println("创建连接成功!");

    3.写sql

     String sql = "select * from userinfo";

    4.得到statement对象执行sql

    statement = connection.prepareStatement(sql);

    5.得到结果集

    rs = statement.executeQuery();

    6.处理结果集

                while(rs.next()){
                    UserInfo userInfo = new UserInfo();
                    userInfo.setId(rs.getInt(1));
                    userInfo.setUserName(rs.getString(2));
                    userInfo.setPassWord(rs.getString(3));
                    list.add(userInfo);
                }

    7.关闭资源

        public static void close(ResultSet rs, Statement statement, Connection connection){
            try {
                if(rs != null)  rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(statement != null) statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(connection != null) connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
  • 相关阅读:
    索引的使用说明
    如何在Linux 发行版本CentOS安装Oracle
    GNU/Linux 初學之旅
    Oracle数据库监听配置(转)
    Linux学习笔记7用户建立密码设置及删除用户
    Linux学习笔记6ls命令
    linux vi命令使用
    生成1千万个随机串号9位英文字母
    郁闷的夏天
    网络爬虫
  • 原文地址:https://www.cnblogs.com/developing/p/11170220.html
Copyright © 2011-2022 走看看