美文网首页
jdbc学习代码

jdbc学习代码

作者: 在努力中 | 来源:发表于2018-06-22 10:20 被阅读0次

从基本连接==》配置文件连接==》连接后操作数据库(更改操作和查询操作)
封装了 连接、关闭连接的方法

package com.lsy.jdbc;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.junit.Test;

public class ReivewTest {
    /**
     * 四、通过 ResultSet 查询结果
     *      1、ResultSet 封装了 jdbc 的查询结果【结果集】
     * @throws Exception 
     */
    @Test
    public void testResultSet() throws Exception{
        // 1、连接数据库
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        String sql = null;
        
        
        try {
            // 2、获取 Statement 对象,通过Connection 的 【createStatement()】 方法获取
            connection = getConnection();
            statement = connection.createStatement();
            
            // 3、准备 SQL 语句
            sql = "select * from customer";
            
            // 4、发送【执行】 SQL 语句,得到结果集,通过Statement 的 【excuteQuery(sql)】 方法
            rs = statement.executeQuery(sql);
            
            // 5、处理结果集,
            //    |5.1、 调用 ResultSet 的 next() 方法,查询下一条记录是否有效,若有效,指针下移
            
            while(rs.next()){

                // |5.2、getXxx() 方法来获得具体列值
                int id = rs.getInt(1);//此处的id是列,值是第几个个列名,也可以直接写列名
                String name = rs.getString("name");
                String email = rs.getString(3);
                Date date = rs.getDate(4);
                System.out.println("id:"+id+"名字"+name);

            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            // 6、关闭数据库资源连接
            release(rs, statement, connection);
        }
        

        
        
    }
    /**
     * 三、利用Statement 对象操作数据库
     * 
     */
    @Test
    public void testStatement(){
        //1.获取数据连接
        Connection connection = null;
        Statement statement = null;
        String sql = null;
        
        try {
            //2.调用Connection 对象的 【createStatement()】 方获取 Statement 对象
            connection = getConnection();
            statement = connection.createStatement();
                
            //3.准备 SQL 语句
            sql = "update customer set name = 'caa' where id = 3";
            
            //4.发送 SQL 语句:调用 Statement 对象的 【executeUpdate(sql)】 方法
            statement.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //5.关闭数据库资源:由里向外关闭
            release(null, statement, connection);
        }
        
        
        
        
    }
    /*
     * 关闭资源
     */
    public void release(ResultSet resultSet,Statement statement,Connection connection){
        
        if(resultSet != null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(statement != null){
            try {
                statement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    /**
     * 二、用配置文件形式 解耦
     * @throws Exception 
     * 
     */
    @Test
    public void testGetConnection2() throws Exception{
        Connection connection = getConnection();
        
        System.out.println("testGetConnection2连接:"+connection);
    }
    private Connection getConnection() throws IOException, ClassNotFoundException, SQLException {
        //0、读取配置文件
        /*
         * 1、属性文件对应Java 中的 Properties类
         *      文件放在src下,实际上是在bin(类所在目录)中
         * 2、可以使用 类加载器 加载 bin 目录 (路径下的文件)
         */
        Properties properties = new Properties();
        InputStream inStream  = 
                this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
        properties.load(inStream);
        
        //1、准备连接数据库的四个字符串:user password,jdbcUrl,driverClass
        // 从配置文件中得到
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("jdbcUrl");
        String driverClass = properties.getProperty("driver");
        
        
        //2、加载驱动:Class.forName(driverClass)
        Class.forName(driverClass);
        
        //3、调用:DriverManager.getConnection(jdbcUrl,user,password)
        //3.1、获取数据库的连接
        Connection connection =
                DriverManager.getConnection(url,user,password);
        return connection;
    }
    
    
    
    /**
     * 一、基础连接
     * Connection 代表应用程序和数据库的连接
     * @throws Exception 
     * 
     */
    @Test
    public void testGetConnection1() throws Exception{
        //1、准备连接数据库的四个字符串:user password,jdbcUrl,driverClass
        String user = "root";
        String password = "123456";
        String url = "jdbc:mysql://localhost:3306/jdbc_test?useSSL=false";
        String driverClass = "com.mysql.jdbc.Driver";
        
        //2、加载驱动:Class.forName(driverClass)
        Class.forName(driverClass);
        
        //3、调用:DriverManager.getConnection(jdbcUrl,user,password)
        //3.1、获取数据库的连接
        Connection connection =
                DriverManager.getConnection(url,user,password);
        
        System.out.println("testGetConnection1连接:"+connection);
    }
}

相关文章

  • jdbc学习代码

    从基本连接==》配置文件连接==》连接后操作数据库(更改操作和查询操作)封装了 连接、关闭连接的方法

  • 009-JDBC,防SQL注入

    DBC JDBC的开发步骤 JDBC代码实现 JDBC实现查询 ORM思想 ORM代码实现 ORM的核心代码 抽取...

  • Spring(5)——Spring 和数据库编程

    传统 JDBC 回顾 JDBC 我们一定不陌生,刚开始学习的时候,我们写过很多很多重复的模板代码: 现在光是看着就...

  • JDBC的简单使用

    JDBC 什么是JDBC 使用java代码发送sql语句的技术,就是jdbc技术。 使用jdbc的前提 需要登录数...

  • java数据库管理之jdbc

    JDBC的开发步骤 注入攻击(不用这个代码编程序) (用这个代码编程序) 更新操作 JDBC的工具类(代码固定) ...

  • JDBC

    JDBC总结: 1.jdbc入门 2.抽取工具类 3.jdbc与java代码联系的基本sql语句操作 4.JDBC...

  • JDBC使用类路径读取jdbc配置文件

    JDBC使用类路径读取jdbc配置文件 时间:20180314 优化jdbc的JdbcUtil类 代码实现在Jdb...

  • JDBC实战教程(二)-优化JDBC配置及代码

    JDBC实战教程-优化JDBC配置及代码 在前面的代码中我们把jdbc配置信息(用户名、密码、驱动信息及连接数据库...

  • JBDC基础--学习笔记(1)

    一.什么是JDBC? 使用java 代码或程序发送sql语句的技术,就是jdbc技术 使用jdbc发送sql前提 ...

  • Spark Sql 以JDBC为数据源

    实现代码 1、准备工作: 2、从JDBC数据读取: 3、 写入数据到JDBC

网友评论

      本文标题:jdbc学习代码

      本文链接:https://www.haomeiwen.com/subject/liahyftx.html