计算机实训课程第四天(2021.7.15)

今天的学习内容为jdbc

在这里插入图片描述

package cn.tedu.jdbc;

/**
 * 作者:陈二胖
 * 时间:2021/7/15  9:43
 * 目的:TODO jdbc
 */

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

/**
 * JDBC:
 * 1.什么是JDBC?
 *   java语言与数据库交互的技术
 *   JDBC是java程序与关系型数据库的交互标准,提供了一套统一的操作界面
 *   JDBC是一组接口,制定jav程序与各种数据库软件交互的一套API
 *      数据库产品:Oracle/mysql/db2/sqlServer
 *   JDBC制定的标准,程序员会根据这套标准,去写java程序
 *   软件运行期间,java程序调用的是数据库厂商提供的具体的API
 *   数据库厂商提供的具体的驱动程序,也就实现了JDBC的标准
 *      JDBC程序--驱动程序----mysql
 *      JDBC程序--驱动程序----oracle
 *      JDBC程序--驱动程序----db2
 *
 * 2.JDBC的编码步骤?
 *   1)加载驱动
 *   2)创建java与数据库的链接
 *   3)发送sql
 *   4)查询等,处理数据
 *   5)关闭连接
 */

//向数据库增加
public class Demo01 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        String className = "com.mysql.jdbc.Driver";
        /**
         * "jdbc:mysql://localhost:3306/tedu_store?useSSL=true";
         * jdbc:mysql: 设置驱动程序类
         * localhost:3306  作为数据库的地址 端口号:3306
         * useSSL=true  高版本的mysql安全性数据交互的选项
         *      想插入中文需要characterEncoding=utf8
         * uesr:数据库的用户名
         * password:数据库密码
         */
        String url = "jdbc:mysql://localhost:3306/tedu_store?characterEncoding=utf8";  //固定写法
        String user = "root";
        String password = "root";
        //1.加载驱动程序
        Class.forName(className);
        System.out.println("加载成功!!!");

        //2.创建连接
        Connection con = DriverManager.getConnection(url,user,password);
        System.out.println(con);
        System.out.println("已经成功与数据库进行建立!!!");

        //3.发送sql语句
        //  Statement语句,发送并执行sql语句
        Statement st = con.createStatement();
        String sql = "INSERT INTO t_user(username,password,email,phone,image,gander,created_user,created_time,modified_user,modified_time\n) " +
                "VALUES ('郑','123123','23333','13733086271',1,1,'1001','2021-07-15','1001','2021-07-15')";  //自己写
        int row = st.executeUpdate(sql);
        if (row>0){
            System.out.println("数据注册成功!");
        }

        //4.关闭
        if (con!=null){
            con.close();
        }
    }
}
package cn.tedu.jdbc;

/**
 * 作者:陈二胖
 * 时间:2021/7/15  9:43
 * 目的:TODO jdbc
 */

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

/**
 * 2.JDBC的编码步骤?
 *   1)加载驱动
 *   2)创建java与数据库的链接
 *   3)发送sql
 *   4)查询等,处理数据
 *   5)关闭连接
 */

//从数据库删除
public class Demo02 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        String className = "com.mysql.jdbc.Driver";
        /**
         * "jdbc:mysql://localhost:3306/tedu_store?useSSL=true";
         * jdbc:mysql: 设置驱动程序类
         * localhost:3306  作为数据库的地址 端口号:3306
         * useSSL=true  高版本的mysql安全性数据交互的选项
         *      想插入中文需要characterEncoding=utf8
         * uesr:数据库的用户名
         * password:数据库密码
         */
        String url = "jdbc:mysql://localhost:3306/tedu_store?characterEncoding=utf8";  //固定写法
        String user = "root";
        String password = "root";
        //1.加载驱动程序
        Class.forName(className);
        System.out.println("加载成功!!!");

        //2.创建连接
        Connection con = DriverManager.getConnection(url,user,password);
        System.out.println(con);
        System.out.println("已经成功与数据库进行建立!!!");

        //3.发送sql语句
        //  Statement语句,发送并执行sql语句
        Statement st = con.createStatement();
        String sql = "DELETE FROM t_user WHERE id = 9";
        int row = st.executeUpdate(sql);
        if (row>0){
            System.out.println("删除成功!");
        }

        //4.关闭
        if (con!=null){
            con.close();
        }
    }
}
package cn.tedu.jdbc;

/**
 * 作者:陈二胖
 * 时间:2021/7/15  9:43
 * 目的:TODO jdbc
 */

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

/**
 * 2.JDBC的编码步骤?
 *   1)加载驱动
 *   2)创建java与数据库的链接
 *   3)发送sql
 *   4)查询等,处理数据
 *   5)关闭连接
 */

//修改密码
public class Demo03 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        String className = "com.mysql.jdbc.Driver";
        /**
         * "jdbc:mysql://localhost:3306/tedu_store?useSSL=true";
         * jdbc:mysql: 设置驱动程序类
         * localhost:3306  作为数据库的地址 端口号:3306
         * useSSL=true  高版本的mysql安全性数据交互的选项
         *      想插入中文需要characterEncoding=utf8
         * uesr:数据库的用户名
         * password:数据库密码
         */
        String url = "jdbc:mysql://localhost:3306/tedu_store?characterEncoding=utf8";  //固定写法
        String user = "root";
        String password = "root";
        //1.加载驱动程序
        Class.forName(className);
        System.out.println("加载成功!!!");

        //2.创建连接
        Connection con = DriverManager.getConnection(url,user,password);
        System.out.println(con);
        System.out.println("已经成功与数据库进行建立!!!");

        //3.发送sql语句
        //  Statement语句,发送并执行sql语句
        Statement st = con.createStatement();
        String sql = "UPDATE t_user SET password = '12312' WHERE id = 7";
        int row = st.executeUpdate(sql);
        if (row>0){
            System.out.println("修改密码成功!");
        }

        //4.关闭
        if (con!=null){
            con.close();
        }
    }
}
package cn.tedu.jdbc;

/**
 * 作者:陈二胖
 * 时间:2021/7/15  9:43
 * 目的:TODO jdbc
 */

import java.sql.*;

/**
 * 2.JDBC的编码步骤?
 *   1)加载驱动
 *   2)创建java与数据库的链接
 *   3)发送sql
 *   4)查询等,处理数据
 *   5)关闭连接
 */

//查询数据库信息
public class Demo04 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        String className = "com.mysql.jdbc.Driver";
        /**
         * "jdbc:mysql://localhost:3306/tedu_store?useSSL=true";
         * jdbc:mysql: 设置驱动程序类
         * localhost:3306  作为数据库的地址 端口号:3306
         * useSSL=true  高版本的mysql安全性数据交互的选项
         *      想插入中文需要characterEncoding=utf8
         * uesr:数据库的用户名
         * password:数据库密码
         */
        String url = "jdbc:mysql://localhost:3306/tedu_store?characterEncoding=utf8";  //固定写法
        String user = "root";
        String password = "root";
        //1.加载驱动程序
        Class.forName(className);
        System.out.println("加载成功!!!");

        //2.创建连接
        Connection con = DriverManager.getConnection(url,user,password);
        System.out.println(con);
        System.out.println("已经成功与数据库进行建立!!!");

        //3.发送sql语句
        //  Statement语句,发送并执行sql语句
        Statement st = con.createStatement();
        String sql = "SELECT * FROM t_user";
        ResultSet rs = st.executeQuery(sql);  //查询信息用executeQuery()

        //4.处理结果集对象
        while (rs.next()){
            String username = rs.getString("username");
            Timestamp created_time = rs.getTimestamp("created_time");
            System.out.println(username + "\t"+ created_time);
        }

        //5.关闭
        if (con!=null){
            con.close();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值