|NO.Z.00080|——————————|BigDataEnd|——|Java&MySQL.JDBC.V05|——|MySQL.v05|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();
    }

}

 
 
 
 
 
 
 
 
 
 

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

 

posted on   yanqi_vip  阅读(17)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示