Database Development three: JDBC database development Getting a (jdbc concept principle)

Articles corresponding video source https://developer.aliyun.com/lesson_1694_13596#_13596

jdbc Getting Started
1 What is jdbc
    jdbc (java DataBase Connectivity) is a java database connectivity, which uses java language to operate the database.
    That we manipulate the database using sql statement in the control box to operate the database, jdbc is to send sql statements to the data using java language .
2jdbc principle

Early sun's genius can connect API to write a database of all the world, when they are just beginning to discover this is mission impossible, because a manufacturer's data is too big library service differentiation. Later sun begin with the database vendors discussion, is the final conclusion is provided by the sun specification (that is, a set of interfaces) to access a database, and provide co-standard connection to the database, then each database vendor will follow the sun to provide a set of rules for access to your company out of the database server API specification provides .sun named jdbc, and various vendors, followed jdbc specification, you can access their data API library is called drive

package jdbc;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
 * Created by kevin on 2020/3/22.
 */
public class Demo1 {
    @Test
    public void fun1() throws Exception{
        /**
         * 一 jdbc四大参数配置:
         * >driverClassName:com.mysql.jdbc.Driver
         * >url:jdbc:mysql://localhost:3306/test
         * >username:root
         * >password:123
         */
        Class.forName("com.mysql.cj.jdbc.Driver");//这是新版本的老版本为com.mysql.jdbc.Driver,MySQL5用的驱动url是com.mysql.jdbc.Driver,MySQL6以后用的是com.mysql.cj.jdbc.Driver.版本不匹配便会报驱动类已过时的错误.
        String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";//JDBC连接MySQL6 (com.mysql.cj.jdbc.Driver), 需要指定时区serverTimezone
        String username = "root";
        String password = "mysql";
        Connection connection = DriverManager.getConnection(url,username,password);
        System.out.println(connection);
        /**
         * 二、怼数据库的增、删、改
         * 1通过Connection对象创建Statement
         * > Statement语句的发送器,它的功能就是向数据库发送sql语句
         * 2调用他的int exexuteUpdate(String sql) 他可以发送DML、DDL
         */
        //1.通过Connection 得到Statement对象
        Statement statement = connection.createStatement();
        String sql = "insert into stu values('0001','张三','100','男')";
//        String sql = "update stu set name='李四' where number='0001'";
//        String sql = "delete from stu where number='0001'";
        int r = statement.executeUpdate(sql);
        System.out.println(r);
        statement.close();
        connection.close();

    }

    /**
     * 执行查询
     */
    @Test
    public void fun2() throws Exception{
        /**
         * 一、得到Connection
         * 二、得到Statement,发送select语句
         * 三、对查询返回的表格进行解析
         * 四、关闭资源
         */
        /**
         * 一、得到连接
         * 1准备四大连接参数
         */
        String driverClassName = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
        String username = "root";
        String password = "mysql";
        /**
         * 2、加载驱动类
         */
        Class.forName(driverClassName);
        /**
         * 3、通过剩下的3个参数调用DrierManger的getConnection得到连接
         */
        Connection connection = DriverManager.getConnection(url,username,password);
        /**
         * 二、获取Statement,执行select语句
         * 1.得到Statement对象:Connection的createStatement())方法
         */
        Statement statement = connection.createStatement();
        /**
         * 2.调用Statement的ResultSet rs = executeQuery(String querySql)
         */
        ResultSet resultSet = statement.executeQuery("select * from stu");
        System.out.println(resultSet);
        /**
         * 三、解析ResultSet
         */
        while(resultSet.next()){
            int gender = resultSet.getInt(3);//通过列编号获取值
            String name = resultSet.getString("name");//通过列名获取值
            System.out.println(name+","+gender);
        }
        /**
         * 四、关闭资源
         * 倒关
         */
        resultSet.close();
        statement.close();
        connection.close();//必须关闭(练习时跑完就结束了,但是真实环境一般项目在一直运行会占用资源,可能引起服务器崩溃)
    }
}

Code associated database

jdbc code for standardization

1, the statement referenced in the definition of the outer try

2, the object is instantiated within the try

3, carried out in the finally closed,

For example:

public void query(){
    Connection con = null;//在try外给出引用定义
    Statement stmt = null;
    ResultSet rs = null;
    try{
        con = xxx;//在try内对象实例化
        while(rs.next()){
            //xxxxxx  
        }
    }catch(Exception e){
        //xxx
    }finally{
        try{
            if(con != null){
                con.close();//在finally中进行关闭
            }
        }
    }

}

 

Published 52 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/YKWNDY/article/details/105029927