JDBC
JDBC连接数据库
大致步骤:
准备jar
mysql-connector-java-5.1.0-bin.jar
加载驱动Class.forName(“com.mysql.jdbc.Driver”);
使用DriverManager获取数据库connection连接
创建Statement对象 用于执行SQL语句
执行SQL语句
释放资源
对应的参数:
数据库驱动
com.microsoft.jdbc.sqlserver.SQLServerDriver(SQL Server)
com.mysql.jdbc.Driver(MySql)
oracle.jdbc.driver.OracleDriver(Oracle)
Statement常用对象
ResultSet executeQuery(sql)
执行sql查询语句,并返回ResultSet对象
int executeUpdate(sql)
执行insert,update,delete语句,返回受影响行数
boolean execute(sql)
执行insert,update,delete语句,返回true或false false成功
防止数据库乱码及日期出错
放入url连接路径之后
?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
PreparedStatement对象使用
防止SQL注入,使用占位符“?”方式进行SQL拼接
首先创建一个实体类:
package Demo.pojo;
import lombok.Data;
@Data //使用lombok替代 get和set方法
public class Brand {
private Integer id;
private String brand_name; //品牌名称
private String company_name; //企业名称
private Integer ordered; //排序字段
private String description; //描述
private Integer status; //启用
@Override
public String toString() {
return "brand{" +
"id=" + id +
", brand_name='" + brand_name + '\'' +
", company_name='" + company_name + '\'' +
", ordered=" + ordered +
", description='" + description + '\'' +
", status=" + status +
'}';
}
}
查询所有信息:
package Demo.JDBCTest;
import Demo.pojo.Brand;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class brandtestSelect {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/tb1";
String username = "数据库用户名";
String password = "数据库密码";
//获取数据库连接
Connection conn = DriverManager.getConnection(url, username, password);
//定义sql
String sql = "select * from tb_brand";
//获取对象
Statement stmt = conn.createStatement();
//执行sql
ResultSet rs = stmt.executeQuery(sql);
//创建brand集合
List<Brand> ls = new ArrayList<>();
//定义brand
Brand brand = null;
while (rs.next()) {
//获取数据
int id = rs.getInt("id");
String brandName = rs.getString("brand_name");
String companyName = rs.getString("company_name");
int ordered = rs.getInt("ordered");
String description = rs.getString("description");
int status = rs.getInt("status");
//封装brand对象
brand = new Brand();
brand.setId(id);
brand.setBrand_name(brandName);
brand.setCompany_name(companyName);
brand.setOrdered(ordered);
brand.setDescription(description);
brand.setStatus(status);
//获取集合
ls.add(brand);
}
//输出集合
System.out.println(ls);
//释放资源
rs.close();
stmt.close();
conn.close();
}
}
插入数据
package Demo.JDBCTest;
import java.sql.*;
public class brandtestInsert {
public static void main(String[] args) throws Exception {
//接收页面提交的参数
String brandName = "香飘飘";
String companyName = "香飘飘有限公司";
int ordered = 20;
String description = "喝过都说好";
int status = 0;
//注册驱动获取地址
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/tb1?characterEncoding=utf-8";
String username = "数据库用户名";
String password = "数据库密码";
//获取数据库连接
Connection conn = DriverManager.getConnection(url, username, password);
//定义sql
//"brand_name" "company_name" "ordered" "description" "status"
String sql = "insert into tb_brand(brand_name,company_name,ordered,description,status) VALUES (?,?,?,?,?);";
//获取对象
PreparedStatement pstmt = conn.prepareStatement(sql);
//设置参数
//其中第一个参数代表第几个 ? 符号
pstmt.setString(1, brandName);
pstmt.setString(2, companyName);
pstmt.setInt(3, ordered);
pstmt.setString(4, description);
pstmt.setInt(5, status);
//执行sql
int count = pstmt.executeUpdate();
//处理结果
System.out.println(count > 0);
pstmt.close();
conn.close();
}
}
这里要注意,创建操作数据库语句的对象时,有两种分别为Statement
和PreparedStatement
区别:
Statement 和 PreparedStatement之间的关系和区别.
关系:PreparedStatement继承自Statement,都是接口
区别:PreparedStatement可以使用 ? 作为占位符,是预编译的,批处理比Statement效率高
在编写sql语句时使用PreparedStatement可以添加问号,并且在一定程度上,防止了SQL注入(拼接字符串)
删除:
package Demo.JDBCTest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class brandtestDelect {
public static void main(String[] args) throws Exception {
//接收页面提交的参数
int id = 4;
//注册驱动获取地址
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/tb1?characterEncoding=utf-8";
String username = "数据库用户名";
String password = "数据库密码";
//获取数据库连接
Connection conn = DriverManager.getConnection(url, username, password);
//定义sql
//"brand_name" "company_name" "ordered" "description" "status"
String sql = "delete from tb_brand where id=?";
//获取对象
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
//执行sql
int count = pstmt.executeUpdate();
//处理结果
System.out.println(count > 0);
pstmt.close();
conn.close();
}
}
修改:
package Demo.JDBCTest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class brandtestUpdate {
public static void main(String[] args) throws Exception {
//接收页面提交的参数
String brandName = "香飘飘";
String companyName = "香飘飘有限公司";
int ordered = 1000;
String description = "没喝过也说好";
int status = 0;
int id = 5;
//注册驱动获取地址
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/tb1?characterEncoding=utf-8";
String username = "数据库用户名";
String password = "数据库密码";
//获取数据库连接
Connection conn = DriverManager.getConnection(url, username, password);
//定义sql
String sql = "update tb_brand \n" +
" set brand_name=?,\n" +
" company_name=?,\n" +
" ordered=?,\n" +
" description=?,\n" +
" status=? \n" +
" where id=?";
//获取对象
PreparedStatement pstmt = conn.prepareStatement(sql);
//设置参数
pstmt.setString(1, brandName);
pstmt.setString(2, companyName);
pstmt.setInt(3, ordered);
pstmt.setString(4, description);
pstmt.setInt(5, status);
pstmt.setInt(6, id);
//执行sql
int count = pstmt.executeUpdate();
//处理结果
System.out.println(count > 0);
pstmt.close();
conn.close();
}
}