jdbc连接mysql,并进行查询、更改以及事物的处理操作

5 篇文章 0 订阅
1 篇文章 0 订阅

1、在eclipse中加入msyql的jar包

右击项目名-> Build Path ->Add Extemal Archives to Java Build Path->找到自己的jar包

2、 连接数据库

加载数据库驱动: Class.forName("com.mysql.jdbc.Driver");
连接数据库:Connection con =DriverManager.getConnection(url,user,password);

3、对数据库进行操作

使用Statement创建对象Statement sta=con.createStatement();
使用preparedState接口实现: preparedStatement pers=prepareStatement(sql);
执行SQL并获得查询结果ResultSet res=sta.executeQuery(sql);
对结果进行处理:
1、使用 next()方法进行遍历:res.next();
2、使用getString()方法 res.getString(x)将其转成字符串输出,x为索引,下标从1开始。

Statement对象的常用方法

在这里插入图片描述

使用Statement对数据库操作

package jdbclianxi;
import java.sql.*;
public class JDBC1 {
	public static void main(String[] args)  {
		Connection con=null;
		Statement  sta=null;
		ResultSet res=null;
		String user="root";
		String  pow="xxxxxx";
		String url="jdbc:mysql://localhost:3306/mydb1?useSSL=false";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			
		}catch(Exception e) {e.printStackTrace();}
		try {
			con =DriverManager.getConnection(url,user,pow);
			sta=con.createStatement();
			String sql1="update t_user set userid=3 where name=2";
			sta.executeUpdate(sql1);
			String sql2="select * from t_user";
			res=sta.executeQuery(sql2);
			while(res.next()) {
				System.out.println(res.getString(1)+"\t"+res.getString(2));
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				con.close();		
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}
}

preparedStatement的常用方法

在这里插入图片描述

使用preparedStatement对数据库进行操作

package jdbclianxi;
import java.sql.*;
public class JDBC3 {
	public static void main(String[] args)  {
		System.out.print("hejeei");
		Connection con=null;
		PreparedStatement sta1=null;
		ResultSet res=null;
		String user="root";
		String  pow="XXXXXX";
		String url="jdbc:mysql://localhost:3306/mydb1?useSSL=false";
		try {
			Class.forName("com.mysql.jdbc.Driver");	
		}catch(Exception e) {e.printStackTrace();}
		try {
			con =DriverManager.getConnection(url,user,pow);
			String sql1="update t_user set userid=3 where name=2";
			String sql2="select * from t_user";
			sta1=con.prepareStatement(sql1);
			sta1.executeUpdate();
			PreparedStatement sta2=con.prepareStatement(sql2);
			res=sta2.executeQuery();
			while(res.next()) {
				System.out.println(res.getString(1)+"\t"+res.getString(2));
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if(res!=null)
				res.close();
				sta1.close();
				con.close();
			}catch (Exception e) {
				e.printStackTrace();
			}	
		}
	}
}


事务

概念: 一组逻辑操作单元(一个或多个DML操作),使数据从一种状态到另一种状态

事务处理的原则

1、要么都执行,要么都不执行(只要有一个操作没有执行就回滚到该事物操作前的状态)使用rollback
2、数据提交(commit)后不能回滚,即每次回滚只能是回到commit之后的数据,得不到commit之前的数据

会自动提交(commit)的操作

1、 DDL操作一旦执行,会自动提交
2、 DML默认情况下是:一旦执行会自动提交;但是我们可以通过set autocommit =false 进行修改,取消其自动提交操作。
3、 在关闭数据库连接时系统也会自动提交

事物的ACID特性

1、原子性
事物是数据库的逻辑工作单位,事物中的操作要么都做,要么都不做
2、一致性
指数据库中只包含成功事物提交的结果
3、隔离性
事物之间互不干扰
4、持续性(即永久性)
事物一旦提交,它对数据库的改变就是永久有效的

通配符的使用

在preparedStatement语句中可以包含多个通配符:“?” 代表字段,通过setxxxx(x,y) x是索引(下标从1开始),y是要填充的内容。

	String sql3="update t_user set userid=? where name=?";
	sta1=con.prepareStatement(sql3);
	sta1.setInt(1, 10);
	sta1.setInt(2, 5);
	sta1.executeUpdate();

事务处理代码

package jdbclianxi;
import java.sql.*;
public class JDBC1 {
	public static void main(String[] args)  {
		System.out.print("hejeei");
		Connection con=null;
		Statement  sta=null;
		PreparedStatement sta1=null;
		String user="root";
		String  pow="XXXXX";
		String url="jdbc:mysql://localhost:3306/mydb1?useSSL=false";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			
		}catch(Exception e) {e.printStackTrace();}
		try {
			con =DriverManager.getConnection(url,user,pow);
			con.setAutoCommit(false);
			sta=con.createStatement();
			String sql1="update t_user set userid=1 where name=2";
			sta.executeUpdate(sql1);
			//System.out.println(10/0); //模拟网络异常
			String sql2="update t_user set userid=2 where name=1";
			sta1=con.prepareStatement(sql1);
			sta.executeUpdate(sql2);
		}catch(Exception e) {
			try {
				con.rollback();
			}catch (Exception e1) {}
			e.printStackTrace();
		}finally {
			try {
				con.commit();		
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值