JDBC 的简单操作(增删改查)

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());
	        	}
        	} 
        }
    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值