JDBC的操作步骤
1.加载数据库驱动
2.通过DriverManager获得连接
3.通过Connection创建Statement对象
4.使用Statement执行SQL语句
5.操作结果集
6.关闭资源
JDBC 的简单操作(增删改查)
增加
在标准SQL语句中insert语句是插入语句,通过Statement接口的executeUpdate()方法可执行插入操作
insert语句的语法格式为 :
INSERT INTO 表名[(字段名1,字段名2…)] VALUES(属性值1,属性值2…)
例如:向数据表tb_emp(包含字段id,name,sex,department)插入数据,代码如下:
insert into tb_emp values(2,’lili’,’女’,’销售部’);
例子
package main;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class insert01 {
public static void main(String arg[]) {
Connection con = null; //定义一个MYSQL链接对象
Statement stmt; //创建声明
try {
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.与数据库建立连接
// DriverManager.getConnection(url,user,password);
/*
#连接数据库的URL
url=jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8
#用户名
user=root
#密码
password=root
*/
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root"); //链接本地MYSQL
//3.通过Connection创建Statement对象
stmt = con.createStatement();
//4.执行SQL语句
String sql = "INSERT INTO user (username, password) VALUES ('init', '123456')";
stmt.executeQuery(sql);
} catch (Exception e) {
System.out.print("MYSQL ERROR:" + e.getMessage());
}finally{
//关闭资源 关闭时应该从最后关闭
if(stmt != null){
try{
stmt.close();
}catch (Exception e) {
System.out.print("MYSQL ERROR:" + e.getMessage());
}
}
if(con!= null){
try{
con.close();
}catch (Exception e) {
System.out.print("MYSQL ERROR:" + e.getMessage());
}
}
}
}
}
修改
在标准SQL语句中,update语句表示更新数据表中记录,JDBC技术通过向数据库发送update语句可实现修改数据操作
update语句的具体语法格式如下:
UPDATE 数据表名 SET 字段名 = 新的字段值 WHERE 条件表达式
例如:将员工tb_emp表中的编号为2的员工姓名修改为“葛雷”,代码如下:
update tb_emp set name = ‘葛雷’ where id = 2;
通过Statement实例的executeUpdate()方法可实现通过Java程序向数据库发送修改SQL语句
例子
package main;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class insert01 {
public static void main(String arg[]) {
Connection con = null; //定义一个MYSQL链接对象
Statement stmt; //创建声明
try {
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.与数据库建立连接
// DriverManager.getConnection(url,user,password);
/*
#连接数据库的URL
url=jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8
#用户名
user=root
#密码
password=root
*/
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root"); //链接本地MYSQL
//3.通过Connection创建Statement对象
stmt = con.createStatement();
//4.执行SQL语句
String sql = "update tb_teacher set name = '陈雨' where id = 2";
stmt.executeQuery(sql);
} catch (Exception e) {
System.out.print("MYSQL ERROR:" + e.getMessage());
}finally{
//关闭资源 关闭时应该从最后关闭
if(stmt != null){
try{
stmt.close();
}catch (Exception e) {
System.out.print("MYSQL ERROR:" + e.getMessage());
}
}
if(con!= null){
try{
con.close();
}catch (Exception e) {
System.out.print("MYSQL ERROR:" + e.getMessage());
}
}
}
}
}
删除
在SQL语句中delete语句用于删除数据,通过使用JDBC技术实现向数据库中发送delete语句可实现删除数据
delete语句的的语法格式如下:
DELETE FROM 数据表名 where 条件表达式
例如:将tb_emp表中编号为1024的员工信息删除 代码如下:
delete from tb_emp where id = 1024;
使用Statement对象的executeUpdate()方法可实现向数据库中发送删除语句
例子
package main;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class insert01 {
public static void main(String arg[]) {
Connection con = null; //定义一个MYSQL链接对象
Statement stmt; //创建声明
try {
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.与数据库建立连接
// DriverManager.getConnection(url,user,password);
/*
#连接数据库的URL
url=jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8
#用户名
user=root
#密码
password=root
*/
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root"); //链接本地MYSQL
//3.通过Connection创建Statement对象
stmt = con.createStatement();
//4.执行SQL语句
String sql = "delete from tb_teacher where id = 4";
stmt.executeQuery(sql);
} catch (Exception e) {
System.out.print("MYSQL ERROR:" + e.getMessage());
}finally{
//关闭资源 关闭时应该从最后关闭
if(stmt != null){
try{
stmt.close();
}catch (Exception e) {
System.out.print("MYSQL ERROR:" + e.getMessage());
}
}
if(con!= null){
try{
con.close();
}catch (Exception e) {
System.out.print("MYSQL ERROR:" + e.getMessage());
}
}
}
}
}
查询
select语句的具体语法格式如下:
SELECT 所选字段列表 FROM 数据表名
WHERE 条件表达式 GROUP BY 字段名 HAVING 条件表达式(指定分组的条件)
ORDER BY 字段名[ASC|DESC]
例如:从员工表tb_emp中所有男员工的姓名、年龄,并按年龄升序排序SQL语句如下:
select name,age formtb_emp where sex = ‘男’ order by age;
例子
package main;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class insert01 {
public static void main(String arg[]) {
Connection con = null; //定义一个MYSQL链接对象
Statement stmt; //创建声明
try {
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.与数据库建立连接
// DriverManager.getConnection(url,user,password);
/*
#连接数据库的URL
url=jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8
#用户名
user=root
#密码
password=root
*/
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root"); //链接本地MYSQL
//3.通过Connection创建Statement对象
stmt = con.createStatement();
//4.执行SQL语句
String sql = "SELECT * FROM user";
ResultSet selectRes = stmt.executeQuery(selectSql);
//5.操作结果集
/*
4种方式:selectRes.getString("username"); selectRes.getString(1);
selectRes.getDouble("age"); selectRes.getDouble(1);
*/
while (selectRes.next()) { //循环输出结果集
String username = selectRes.getString("username");
String password = selectRes.getString("password");
System.out.print("\r\n\r\n");
System.out.print("username:" + username + "password:" + password);
}
} catch (Exception e) {
System.out.print("MYSQL ERROR:" + e.getMessage());
}finally{
//关闭资源 关闭时应该从最后的开始关闭
if(selectRes != null){
try{
selectRes .close();
}catch (Exception e) {
System.out.print("MYSQL ERROR:" + e.getMessage());
}
}
if(stmt != null){
try{
stmt.close();
}catch (Exception e) {
System.out.print("MYSQL ERROR:" + e.getMessage());
}
}
if(con!= null){
try{
con.close();
}catch (Exception e) {
System.out.print("MYSQL ERROR:" + e.getMessage());
}
}
}
}
}