【Java学习】JavaWeb ---- JDBC

文章目录

    • JDBC 快速入门
    • ResultSet
    • 数据连接池

JDBC 快速入门

  1. 下载jar包(百度)->add as library
    【Java学习】JavaWeb ---- JDBC_第1张图片
  2. 代码
package com.ith.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class demo1 {
    public static void main(String[] args) throws Exception {
        //1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.获取连接
        String url="jdbc:mysql://127.0.0.1:3306/db1";
        String username = "root";
        String password = "****";
        Connection conn = DriverManager.getConnection(url,username,password);

        //3.定义sql
        String sql = "UPDATE sc SET Grade = 6 WHERE Sno = 1";
        // 创建数据库
        // String sql = "create database db2";

        //4.获取执行sql的对象 Statement
        Statement stmt = conn.createStatement();

        //5.执行sql
        int count = stmt.executeUpdate(sql);

        //6.处理结果
        System.out.println(count);

        //7.释放资源
        stmt.close();
        conn.close();
    }
}

输出:1
表示有一行受影响
3. 数据库更新成功
【Java学习】JavaWeb ---- JDBC_第2张图片

ResultSet

  • 结果集对象,封装了DQL查询语句的结果
  • 获取查询结果:boolean next()、xxx getXxx(参数):参数(int 列的编号、String 列的名称)

代码

package com.ith.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class demoResultSet {
    public static void main(String[] args) throws Exception {
        //1.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
//        String url="jdbc:mysql://127.0.0.1:3306/db1";
        String url = "jdbc:mysql:///db1";
        String username = "root";
        String password = "****";
        Connection conn = DriverManager.getConnection(url, username, password);

        //3.定义sql
        String sql = "select * from sc";

        //4.获取Statement
        Statement stmt = conn.createStatement();

        //5.执行
        ResultSet rs = stmt.executeQuery(sql);

        //6.遍历rs
        while(rs.next()){
            //获取数据
            String Sno = rs.getString("Sno");//可以用列名
            int Cno = rs.getInt(2);//可以用编号
            int Grade = rs.getInt("Grade");

            System.out.println("Sno: "+Sno);
            System.out.println("Cno: "+Cno);
            System.out.println("Grade: "+Grade);

            System.out.println("------------------------");
        }
        //7.释放资源
        rs.close();
        conn.close();
        stmt.close();
    }
}
/**
 * 输出:
 Sno: 12
 Cno: 1
 Grade: 5
 ------------------------
 Sno: 1234
 Cno: 123
 Grade: 3
 ------------------------
 Sno: 123
 Cno: 12
 Grade: 4
 ------------------------
 Sno: 1
 Cno: 0
 Grade: 6
 ------------------------

 */

ResultSet存入集合

package com.ith.pojo;

public class SC {
    private String Sno;
    private int Cno;
    private int Grade;

    @Override
    public String toString() {
        return "SC{" +
                "Sno=" + Sno +
                ", Cno=" + Cno +
                ", Grade=" + Grade +
                '}';
    }


    public String getSno() {
        return Sno;
    }

    public void setSno(String sno) {
        Sno = sno;
    }

    public int getCno() {
        return Cno;
    }

    public void setCno(int cno) {
        Cno = cno;
    }

    public int getGrade() {
        return Grade;
    }

    public void setGrade(int grade) {
        Grade = grade;
    }
}

package com.ith.pojo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class SCdemo {
    public static void main(String[] args) throws Exception {
        //1.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
//        String url="jdbc:mysql://127.0.0.1:3306/db1";
        String url = "jdbc:mysql:///db1";
        String username = "root";
        String password = "****";
        Connection conn = DriverManager.getConnection(url, username, password);

        //3.定义sql
        String sql = "select * from sc";

        //4.获取Statement
        Statement stmt = conn.createStatement();

        //5.执行
        ResultSet rs = stmt.executeQuery(sql);

        //创建集合
        List<SC> list = new ArrayList<>();

        //6.遍历rs
        while(rs.next()){
            //创建对象
            SC sc = new SC();


            //获取数据
            String Sno = rs.getString("Sno");//可以用列名
            int Cno = rs.getInt(2);//可以用编号
            int Grade = rs.getInt("Grade");

            sc.setSno(Sno);
            sc.setCno(Cno);
            sc.setGrade(Grade);

            //存入集合
            list.add(sc);
        }

        System.out.println(list);
        //7.释放资源
        rs.close();
        conn.close();
        stmt.close();
    }
}
/**
输出:
[SC{Sno=12, Cno=1, Grade=5}, SC{Sno=1234, Cno=123, Grade=3}, SC{Sno=123, Cno=12, Grade=4}, SC{Sno=1, Cno=0, Grade=6}]
*/


数据连接池

  • 是个容器,负责分配、管理数据库连接(Connection)
  • 使用已有的连接
  • 释放空闲连接
  • 好吃:资源复用、提高响应速度、避免数据库连接遗漏

Driud数据连接池

导入jar包

【Java学习】JavaWeb ---- JDBC_第3张图片

package com.ith.druid;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.util.Properties;

public class Druiddemo {
    public static void main(String[] args) throws Exception {
        //1.导入jar包

        //2.配置

        //3.加载配置文件
        Properties prop = new Properties();
        prop.load((new FileInputStream("jdbc-demo/src/druid.properties")));
        //4.获取连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);

        //5.获取数据库连接Connection
        Connection connection = dataSource.getConnection();

        System.out.println(connection);
        //System.out.println(System.getProperty("user.dir"));
    }
}

你可能感兴趣的:(java学习,java,学习,数据库)