1、Statement接口引入
作用:用于执行静态 SQL 语句并返回它所生成结果的对象。
常用方法:
int executeUpdate(String sql)
执行给定 SQL 语句,该语句可能为 INSERT、UPDATE 或DELETE 语句,或者不返回任何内容的 SQL 语句(如 SQL DDL 语句)。void close()
立即释放此 Statement 对象的数据库和 JDBC 资源,而不是等待该对象自动关闭时发生此操作。
2、使用 Statement 接口实现添加数据操作
实例1:
创建工具类DbUtil:
import java.sql.Connection;
import java.sql.DriverManager;
public class DbUtil {
private static String dbUrl="jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=UTF-8";
private static String dbUserName="root";
private static String dbPassword="root";
private static String jdbcName="com.mysql.jdbc.Driver";
/**
* 获取数据库连接
* @return
* @throws Exception
*/
public Connection getCon() throws Exception{
Class.forName(jdbcName);
Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
return con;
}
/**
* 关闭连接
* @param con
* @throws Exception
*/
public void close(Connection con)throws Exception{
if(con!=null){
con.close();
}
}
}
import java.sql.Connection;
import java.sql.Statement;
import com.cn.jdbc.Util.DbUtil;
public class jdbcTest {
public static void main(String[] args) throws Exception {
DbUtil dbUtil=new DbUtil();
String sql="insert into student values(null,'东方不败','女',1000) ";
Connection con=dbUtil.getCon();//获取数据库连接
Statement stmt=con.createStatement();//获取Statement
int result=stmt.executeUpdate(sql);//判断是否成功
System.out.println("操作的结果:"+result+"数据");
stmt.close();//关闭Statement
con.close();//关闭连接
}
}
运行结果:
实例2:使用变量,面向对象的方法
1、创建学生模型
public class Student {
private String stuName;
private String stuSex;
private int stuAge;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String stuName, String stuSex, int stuAge) {
super();
this.stuName = stuName;
this.stuSex = stuSex;
this.stuAge = stuAge;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuSex() {
return stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
}
2、工具类DbUtil
public class DbUtil {
private static String jdbcName="com.mysql.jdbc.Driver";
private static String dbUrl="jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=UTF-8";
private static String dbUserName="root";
private static String dbPassword="root";
public Connection getCon() throws Exception{
Class.forName(jdbcName);
Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
return con;
}
public void close(Statement stmt,Connection con) throws Exception{
if(stmt!=null){
stmt.close();
if(con!=null){
con.close();
}
}
}
}
3、测试类
public class JDBCTest {
private static DbUtil dbUtil=new DbUtil();
Student student=new Student();
/**
* 添加学生
* @param student
* @return
* @throws Exception
*/
public static int addStudent(Student student)throws Exception{
Connection con=dbUtil.getCon();//获取连接
String sql="insert into student values(null,'"+student.getStuName()+"','"+student.getStuSex()+"',"+student.getStuAge()+")";
Statement stmt=con.createStatement();//创建Statement
int result=stmt.executeUpdate(sql);
dbUtil.close(stmt, con);//关闭Statement和连接
return result;
}
public static void main(String[] args)throws Exception {
Student student=new Student("三娃","男",77);
int result=addStudent(student);
if(result==1){
System.out.println("成功");
}else{
System.out.println("失败");
}
}
}
3、使用 Statement 接口实现更新数据操作
实例1:通过ID更新数据
1、创建学生模型
public class Student {
private int id;
private String stuName;
private String stuSex;
private int stuAge;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int id, String stuName, String stuSex, int stuAge) {
super();
this.id = id;
this.stuName = stuName;
this.stuSex = stuSex;
this.stuAge = stuAge;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuSex() {
return stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
2、工具类DbUtil
public class DbUtil {
private static String dbUrl="jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=UTF-8";
private static String dbUserName="root";
private static String dbPassword="root";
private static String jdbcName="com.mysql.jdbc.Driver";
/**
* 获取数据库连接
* @return
* @throws Exception
*/
public Connection getCon() throws Exception{
Class.forName(jdbcName);
Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
return con;
}
/**
* 关闭连接
* @param con
* @throws Exception
*/
public void close(Statement stmt,Connection con)throws Exception{
if(stmt!=null){
stmt.close();
if(con!=null){
con.close();
}
}
}
}
3、测试类
public class jdbcTest {
private static DbUtil dbUtil=new DbUtil();
private static Student student=new Student();
/**
* 更新数据
* @param stuName
* @param stuSex
* @param stuAge
* @return
*/
public static int updateStudent(Student student)throws Exception{
Connection con=dbUtil.getCon();//获取连接
String sql="update student set stuName='"+student.getStuName()+"',stuSex='"+student.getStuSex()+"',stuAge="+student.getStuAge()+" where stuId="+student.getId()+"";
Statement stmt=con.createStatement();//创建Statement
int result=stmt.executeUpdate(sql);//判断是否成功
dbUtil.close(stmt, con);//关闭Statement和连接
return result;
}
public static void main(String[] args) throws Exception {
Student student=new Student(1,"擎天柱","男",999);
updateStudent(student);
}
}
运行结果:
第四节:使用 Statement 接口实现删除数据操作
实例1:
3、测试类
public class jdbcTest {
private static DbUtil dbUtil=new DbUtil();
private static Student student=new Student();
/**
* 删除数据
* @param stuName
* @param stuSex
* @param stuAge
* @return
*/
public static int deleteStudent(int id)throws Exception{
Connection con=dbUtil.getCon();//获取连接
String sql="delete from student where stuId="+id;
Statement stmt=con.createStatement();//创建Statement
int result=stmt.executeUpdate(sql);//判断是否成功
dbUtil.close(stmt, con);//关闭Statement和连接
return result;
}
public static void main(String[] args) throws Exception {
deleteStudent(1);
}
}
运行结果: