1.statement接口引入-----用于执行静态SQL语句返回所生成结果的对象。
2.int executeUpdate(String sql)-----支持增删改
void close(); 关闭数据库连接
封装方法--
package jdbc.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class DbUtil {
//数据库地址
private static String dbUrl="jdbc:mysql://localhost:3306/mybatis_db";
//用户名
private static String dbUserName="root";
//密码
private static String dbPassword="***********";
//驱动名称
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();
}
}
}
}
方法一:面向对象
package jdbc.chap03;
import java.sql.Connection;
import java.sql.Statement;
import jdbc.util.DbUtil;
public class Demo02 {
private static DbUtil dbUtil=new DbUtil();
/**
* 添加个人信息
* @param personName
* @param age
* @return
* @throws Exception
*/
private static int addinformation(String personName,Integer age)throws Exception{
Connection con=dbUtil.getCon();//获取连接
String sql="insert into t_student values(656,'"+personName+"',"+age+")";
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{
int result=addinformation("Allen", 43);
if(result==1){
System.out.println("添加成功了!");
}else {
System.out.println("添加失败!");
}
}
}
结果::
方法二:
package jdbc.chap03;
import java.sql.Connection;
import java.sql.Statement;
import jdbc.util.DbUtil;
public class Demo01 {
public static void main(String[] args) throws Exception{
DbUtil dbUtil=new DbUtil();
String sql="insert into t_student values(233,'文科',21)";
Connection con=dbUtil.getCon();//获取数据库连接
Statement stmt=con.createStatement();//获取Statement
int result=stmt.executeUpdate(sql);
System.out.println("操作的结果:"+result+"数据");
stmt.close();//关闭statement
con.close();//关闭连接
}
}
方法三:面向对象
package jdbc.chap03;
import java.sql.Connection;
import java.sql.Statement;
import jdbc.util.DbUtil;
import model.Person;
public class Demo02 {
private static DbUtil dbUtil=new DbUtil();
/**
* 添加个人信息2
* @param person
* @return
* @throws Exception
*/
private static int add2(Person person)throws Exception{
Connection con=dbUtil.getCon();//获取连接
String sql="insert into t_student values(null,'"+person.getPersonName()+"',"+person.getAge()+")";
Statement stmt=con.createStatement();//创建Statement
int result=stmt.executeUpdate(sql);
dbUtil.close(stmt, con);//关闭Statement和连接
return result;
}
/**
* 添加个人信息
* @param personName
* @param age
* @return
* @throws Exception
*/
/*private static int addinformation(String personName,Integer age)throws Exception{
Connection con=dbUtil.getCon();//获取连接
String sql="insert into t_studeMnt values(654,'"+personName+"',"+age+")";
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{
/*int result=addinformation("Allen", 43);
if(result==1){
System.out.println("添加成功了!");
}else {
System.out.println("添加失败!");
}*/
//多行注释 ctrl+shift+/
Person person=new Person("周星驰",28);
int result=add2(person);
if(result==1){
System.out.println("添加成功了!");
}else {
System.out.println("添加失败!");
}
}
}
person类:
package model;
/**
* 个人信息
* @author MC-DS
*
*/
public class Person {
private int id;
private String personName;
private int age;
public Person(String personName, int age) {
super();
this.personName = personName;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public Integer getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}