|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
分类:
bdv005-mysql
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通