|NO.Z.00080|——————————|BigDataEnd|——|Java&MySQL.JDBC.V05|------------------------------------------|

2022/4/6 19:19:36

本文主要是介绍|NO.Z.00080|——————————|BigDataEnd|——|Java&MySQL.JDBC.V05|------------------------------------------|,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!



[BigDataJava:Java&MySQL_JDBC.V05]                                                                [BigDataJava.MySQL基础][|章节五|mysql基础|Jdbc|Jdbc开发_获取语句执行对象|]








一、[JDBC开发_获取语句执行对象]:API使用:获取语句执行平台
### --- [JDBC开发_获取语句执行对象]:API使用:获取语句执行平台

~~~     通过Connection 的 createStatement方法 获取sql语句执行对象
Connection接口中的方法说明
Statement createStatement() 创建 SQL语句执行对象
### --- Statement : 

~~~     代表一条语句对象,用于发送 SQL 语句给服务器,
~~~     用于执行静态 SQL 语句并返回它所生成结果的对象。
Statement类 常用方法说明
int executeUpdate(String sql);执行insert update delete语句.返回int类型,代表受影响的行数
ResultSet executeQuery(Stringsql);执行select语句, 返回ResultSet结果集对象
二、代码示例
public class JDBCDemo03 {
public static void main(String[] args) throws Exception {
    
    //1.注册驱动
    Class.forName("com.mysql.jdbc.Driver");
    
    //2.获取连接 url,用户名, 密码
    String url = "jdbc:mysql://localhost:3306/db4";
    Connection con = DriverManager.getConnection(url, "root", "123456");
    
    //3.获取 Statement对象
    Statement statement = con.createStatement();

    //4.执行创建表操作
    String sql = "create table test01(id int, name varchar(20),age int);";

    //5.增删改操作 使用executeUpdate,增加一张表

    int i = statement.executeUpdate(sql);

    //6.返回值是受影响的函数
    System.out.println(i);

    //7.关闭流
    statement.close();
    con.close();
    }
}
三、sql语句
package com.yanqi.jdbc05;

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

public class JdbcDemo01 {

    public static void main(String[] arges) throws Exception {

        //1、注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        //2、获取连接connection连接对象com.mysql.jdbc.JDBC4Connection@188715b5
        String url = "jdbc:mysql://localhost:3306/db4?characterEncoding=UTF-8";
        Connection con = DriverManager.getConnection(url, "root", "123456");

        //3、打印连接对象
        System.out.println(con);

        //3.获取语句执行平台 Statement
        Statement statement = con.createStatement();

        //3.1 通过 statement对象的 executeUpdate 方法 创建一张表
        String sql = "create table test(id int,name varchar(20),age int);";
        int i = statement.executeUpdate(sql); // 返回值是int类型 表示受影响的行数

        System.out.println(i);

        //4.关闭流
        statement.close();
        con.close();
    }

}








===============================END===============================


Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart                                                                                                                                                   ——W.S.Landor



来自为知笔记(Wiz)



这篇关于|NO.Z.00080|——————————|BigDataEnd|——|Java&MySQL.JDBC.V05|------------------------------------------|的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程