zoukankan      html  css  js  c++  java
  • 传统JDBC操作数据库

    package com.jdbc.example;
    
    import java.sql.Connection;
    import java.sql.Date;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class JdbcExample {
    
        //创建一个数据库的连接
        private static Connection getConnection() {
            Connection connection = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");//加载用户驱动
                String url = "jdbc:mysql://localhost:3306/mybatis";//连接数据库的地址
                String user = "root";//数据库的用户名
                String password = "root";//数据库的密码
                connection = DriverManager.getConnection(url, user, password);//得到一个数据库的连接
            } catch (ClassNotFoundException e) {
                // TODO 自动生成的 catch 块
                System.out.println(JdbcExample.class.getName() + "数据库驱动包未找到!");
                return null;
            } catch (SQLException e) {
                // TODO 自动生成的 catch 块
                System.out.println(JdbcExample.class.getName() + "SQL语句有问题,无法查询成功!");
                return null;
            }
            return connection;//返回该连接
        }
    
    
        public User getUser(int id) {
            Connection connection = getConnection();//得到该数据库的连接
            PreparedStatement ps = null;//声明一个null的预处理的Statement
            ResultSet rs = null;//声明一个结果集,用来存放SQL的查询后的结果
            try {
                ps = connection.prepareStatement("select * from user where id=?");//对查询的User表的SQL进行预处理编译
                ps.setInt(1, id);//把参数Id设值到数据的条件中
                rs = ps.executeQuery();//执行查询语句。把结果返回到ResultSet结果集中
                while (rs.next()) {//遍历从结果集中取数
                    int user_id = rs.getInt("id");//取出Statement的用户id
                    String username = rs.getString("username");//取出Statement的用户名
                    Date birthday = rs.getDate("birthday");//取出Statement的生日
                    String sex = rs.getString("sex");//取出Statement的性别
                    String address = rs.getString("address");//取出Statement的用户地址
    
                    User user = new User();//创建一个User类的实体对象POJO
                    user.setId(user_id);//存放在user对象中
                    user.setUsername(username);
                    user.setBirthday(birthday);
                    user.setSex(sex);
                    user.setAddress(address);
    
                    return user;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                this.close(rs, ps, connection);
            }
            return null;
        }
    
        //判断数据库是否关闭
        /***
         * 
         * @param rs 查看结果集是滞关闭
         * @param stmt 预处理SQL是否关闭
         * @param conn 数据库连接是否关闭
         */
        private void close(ResultSet rs, Statement stmt, Connection conn) {
    
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
                System.out.println(JdbcExample.class.getName() + "ResultSet 关闭失败!");
            }
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
                System.out.println(JdbcExample.class.getName() + "Statement 关闭失败!");
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                System.out.println(JdbcExample.class.getName() + "Connection 关闭失败!");
            }
        }
    
    
    
        public static void main(String[] args) {
            User user = new JdbcExample().getUser(1);//我们查询用户的id 为 1 用户 
            System.out.println(user);//打印输出查询出来的数据
    
        }
    
    }
  • 相关阅读:
    ntohs, ntohl, htons,htonl的比较和详解【转】
    Device Tree 详解【转】
    浅析Linux DeviceTree【转】
    【spring boot】spring boot 拦截器
    【jQuery】jQuery/js 判断字符串是否JSON字符串
    【java】java中的 &= 和 |= 和 ^= 的区别
    zabbix创建触发器、action,发送报警邮件
    html iframe禁用右键
    mysql数据库mysqldump方式备份
    JDK8新特性
  • 原文地址:https://www.cnblogs.com/xiaoxiaoccaiya/p/9377043.html
Copyright © 2011-2022 走看看