使用Statement执行sql的对象完成数据库添加、修改、删除动作。
数据库添加数据:
语法
语法:insert into 表名 values(值1,值2,…值n)
package cn.itcast.jbdc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/*
account表 添加一条记录 insert 语句
*/
public class JDBCDemo02 {
public static void main(String[] args) {
Statement stmt = null;
Connection conn = null;
try {
//1.注册驱动
Class.forName(“com.mysql.jdbc.Driver”);
//2.定义sql
String sql = “insert into account values(null,‘王五’,3000)”;
//3.获取Connection对象
conn = DriverManager.getConnection(“jdbc:mysql:///bd3”, “root”, “root”);
//4.获取执行sql的对象 Statement
stmt = conn.createStatement();
//5.执行sql
int count = stmt.executeUpdate(sql); //影响的行数
//6.处理结果
System.out.println(count);
if (count>0){
System.out.println(“添加成功!”);
}else{
System.out.println(“添加失败!”);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
//7.释放资源
//stmt.close();
//避免空指针异常
if (stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
一、运行结果:
添加成功!
二、数据库未执行JDBC添加操作之前
**#**数据库执行JDBC添加操作之后:
数据库修改数据:
语法:update 表名 set 列名1 = 值1,列名2 = 值2,…[where 条件];
package cn.itcast.jbdc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/*
account表 修改一条记录
*/
public class JDBCDemo03 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
//1.注册驱动
Class.forName(“com.mysql.jdbc.Driver”);
//2.获取连接对象
conn = DriverManager.getConnection(“jdbc:mysql:///bd3”, “root”, “root”);
//3.定义sql
String sql = “update account set balance = 1500 where id = 3”;
//4.获取执行sql对象
stmt = conn.createStatement();
//5.执行sql语句
int count = stmt.executeUpdate(sql);
//6.处理结果
System.out.println(count);
if (count>0){
System.out.println(“修改成功!”);
}else{
System.out.println(“修改失败!”);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
//7.释放资源
//stmt.close();
//避免空指针异常
if (stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
运行结果:
修改成功!
三、数据库未执行JDBC修改操作之前:
四、数据库执行JDBC修改操作之后:
五、数据库删除数据:
语法:delete from 表名 [where 条件];
package cn.itcast.jbdc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/*
account表 删除一条记录
*/
public class JDBCDemo04 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
//1.注册驱动
Class.forName(“com.mysql.jdbc.Driver”);
//2.获取连接对象
conn = DriverManager.getConnection(“jdbc:mysql:///bd3”, “root”, “root”);
//3.定义sql
String sql = “delete from account where id = 3”;
//4.获取执行sql对象
stmt = conn.createStatement();
//5.执行sql语句
int count = stmt.executeUpdate(sql);
//6.处理结果
System.out.println(count);
if (count>0){
System.out.println(“删除成功!”);
}else{
System.out.println(“删除失败!”);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
//7.释放资源
//stmt.close();
//避免空指针异常
if (stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
运行结果:
删除成功!