Basic operations of JDBC entry-level addition, deletion, modification, and query, as well as exception throwing and data rollback

//增加,throws Exception抛出异常
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class text01_01 {

	public static void main(String[] args) throws Exception {
        //基本信息
		String url="jdbc:mysql://localhost:13306/stu";//13306为MySQL接口,stu为库名
		String user="root";
		String password="1234";
		
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		
		//获取连接
		Connection conn=DriverManager.getConnection(url, user, password);
		
		//预编译,返回PreparedStatement实例
		String sql="insert into stu1(xh,name,cj) values(?,?,?)";//stu1为表名
		PreparedStatement pst=conn.prepareStatement(sql);
		
		//填写占位符(?)
		pst.setInt(1, 1);//第一个?为xh(学号int) 设置元素为:1
		pst.setString(2, "张三");//第二个?name(姓名varchar) 设置元素为:张三
		pst.setInt(3,60);//第三个?cj(成绩int) 设置元素为:60
		
		//执行操作
		pst.execute();
		/*int n=pst.executeUpdate();//执行操作增删改,并返回一个int型数值,表示同步更新的条数
		if(n>0){
			System.out.println("增加了"+n+"个数据");
		}else{
			System.out.println("增加未成功");
		}*/
		
		//关闭资源
		conn.close();
		pst.close();
	}
}




//try catch抓住异常,增删改查均可使用
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class text01_01 {

	public static void main(String[] args){
		
		Connection conn=null;
		PreparedStatement pst=null;//定义全局,方便finally使用

		try {
			//基本信息
			String url="jdbc:mysql://localhost:13306/stu";//13306为MySQL接口,stu为库名
			String user="root";
			String password="1234";
			
			//加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			
			//获取连接
			conn=DriverManager.getConnection(url, user, password);
			
			//预编译,返回PreparedStatement实例
			String sql="insert into stu1(xh,name,cj) values(?,?,?)";//stu1为表名
			pst=conn.prepareStatement(sql);
			
			//填写占位符(?)
			pst.setInt(1, 1);//第一个?为xh(学号int) 设置元素为:1
			pst.setString(2, "张三");//第二个?name(姓名varchar) 设置元素为:张三
			pst.setInt(3,60);//第三个?cj(姓名int) 设置元素为:75
			
			//执行操作
			pst.execute();
			/*int n=pst.executeUpdate();//执行操作增删改,并返回一个int型数值,表示同步更新的条数
			if(n>0){
				System.out.println("增加了了"+n+"个数据");
			}else{
				System.out.println("增加未成功");
			}*/
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//释放资源
			if(pst!=null){
				try {
					pst.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}	
		}	
	}//注:try catch为抓住异常,避免前面无法进行而导致后面资源未关闭  比throws Exception好
}//try里面有异常则进入catch里面
//删除
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class text01_02 {

	public static void main(String[] args) throws Exception {
        //基本信息
		String url="jdbc:mysql://localhost:13306/stu";//stu为库名
		String user="root";
		String password="1234";
		
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		
		//获取连接
		Connection conn=DriverManager.getConnection(url, user, password);
		
		//预编译,返回PreparedStatement实例
		String sql="delete from stu1 where xh=?";//stu1为表名
		PreparedStatement pst=conn.prepareStatement(sql);
		
		//填写占位符(?)
		pst.setInt(1, 1);
		
		//执行操作
		//pst.execute();//直接执行
		int n=pst.executeUpdate();//执行操作增删改,并返回一个int型数值,表示同步更新的条数
		if(n>0){
			System.out.println("删除了"+n+"个数据");
		}else{
			System.out.println("删除未成功");
		}
		
		//关闭资源
		conn.close();
		pst.close();
	}
}



//删除
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class text01_02 {

	public static void main(String[] args){
		Connection conn=null;
		PreparedStatement pst=null;
		
		try {
			//基本信息
			String url="jdbc:mysql://localhost:13306/stu";//stu为库名
			String user="root";
			String password="1234";
			
			//加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			
			//获取连接
			conn=DriverManager.getConnection(url, user, password);
			
			//预编译,返回PreparedStatement实例
			String sql="delete from stu1 where xh=?";//stu1为表名
			pst=conn.prepareStatement(sql);
			
			//填写占位符(?)
			pst.setInt(1, 1);
			
			//执行操作
			//pst.execute();//直接执行
			int n=pst.executeUpdate();//执行操作增删改,并返回一个int型数值,表示同步更新的条数
			if(n>0){
				System.out.println("删除了"+n+"个数据");
			}else{
				System.out.println("删除未成功");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(pst!=null){
				try {
					pst.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
//修改
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class text01_04 {

	public static void main(String[] args) throws Exception {
        //基本信息
		String url="jdbc:mysql://localhost:13306/stu";//stu为库名
		String user="root";
		String password="1234";
		
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		
		//获取连接
		Connection conn=DriverManager.getConnection(url, user, password);
		
		//预编译,返回PreparedStatement实例
		String sql="update stu1 set cj=? where xh<?";//stu1为表名
		PreparedStatement pst=conn.prepareStatement(sql);
		
		//填写占位符(?)
		pst.setInt(1, 3);
		pst.setInt(2, 8);//把学号小于8的成绩改为3
		
		//执行操作
		//pst.execute();//直接执行
		int n=pst.executeUpdate();//执行操作增删改,并返回一个int型数值,表示同步更新的条数
		if(n>0){
			System.out.println("修改了"+n+"个数据");
		}else{
			System.out.println("修改未成功");
		}
		
		//关闭资源
		conn.close();
		pst.close();
	}
}



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class text01_04 {

	public static void main(String[] args){
		Connection conn=null;
		PreparedStatement pst1=null;
		PreparedStatement pst2=null;
		
		try {
			//基本信息
			String url="jdbc:mysql://localhost:13306/stu";//stu为库名
			String user="root";
			String password="1234";
			
			//加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			
			//获取连接
			conn=DriverManager.getConnection(url, user, password);
			
			//开启事务
			conn.setAutoCommit(false);//默认为false
			
			//预编译,返回PreparedStatement实例
			String sql1="update stu1 set cj=? where xh=?";//stu1为表名
			String sql2="update stu1 set cj=? where xh=?";//stu1为表名
			pst1=conn.prepareStatement(sql1);
            pst2=conn.prepareStatement(sql2);
			
			//填写占位符(?)
			pst1.setInt(1, 3);
			pst1.setInt(2, 8);//把学号等于8的成绩改为3
			  //手动制造异常
			   int i=1/0;
			pst2.setInt(1, 5);//由于上一行异常,下面未执行因此只修改了pst1
			pst2.setInt(2, 1);
			
			
			//执行操作
			//pst.execute();//直接执行
			pst1.executeUpdate();//执行操作增删改,并返回一个int型数值,表示同步更新的条数
			
			//提交事务
			conn.commit();
		} catch (Exception e) {
			//事务回滚
			if(conn!=null){
				try {
					conn.rollback();//如果没有完全执行则回滚数据,避免一个执行,一个未执行造成麻烦
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			e.printStackTrace();
		}finally{
			//释放资源
			if(pst1!=null){
				try {
					pst1.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			
			if(pst2!=null){
				try {
					pst2.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}	
		}
	}
}
//查询
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class text01_03 {

	public static void main(String[] args) throws Exception {
        //基本信息
		String url="jdbc:mysql://localhost:13306/stu";//stu为库名
		String user="root";
		String password="1234";
		
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		
		//获取连接
		Connection conn=DriverManager.getConnection(url, user, password);
		
		//预编译,返回PreparedStatement实例
		String sql="select * from stu1 where xh=?";//stu1为表名
		PreparedStatement pst=conn.prepareStatement(sql);
		
		//填写占位符(?)
		pst.setInt(1, 3);
		
		//执行操作
		//pst.execute();//直接执行
		ResultSet rs=pst.executeQuery();//执行操作查,并返回一个ResultSet实例,为结果集
		/*ResultSet的作用:1.first()指针移到第一行;结果集为空返回false,否则true
                           2.last()指针移到第一行;结果集为空返回false,否则true
		                   3.previous()指针移到上一行;若存在上一行true,否则false
		                   4.next()指针移到下一行;若存在下一行true,否则false
		                   5.getRow()返回int类型的索引,从1开始
		*/
		System.out.println("索引 学号 姓名 成绩");
		while(rs.next())
		{
			int a=rs.getRow();
			int b=rs.getInt("xh");
			String c=rs.getString("name");
			int d=rs.getInt("cj");
			System.out.println(a+"   "+b+"   "+c+"   "+d);
		}
		
		//关闭资源
		conn.close();
		pst.close();
	}
}




import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class text01_03 {

	public static void main(String[] args){
		Connection conn=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		
		try {
			//基本信息
			String url="jdbc:mysql://localhost:13306/stu";//stu为库名
			String user="root";
			String password="1234";
			
			//加载驱动
			Class.forName("com.mysql.jdbc.Driver");

			//获取连接
			conn=DriverManager.getConnection(url, user, password);
			
			//预编译,返回PreparedStatement实例
			String sql="select * from stu1 where xh=?";//stu1为表名
			pst=conn.prepareStatement(sql);
			
			//填写占位符(?)
			pst.setInt(1, 3);
			
			//执行操作
			//pst.execute();//直接执行
			rs=pst.executeQuery();//执行操作查,并返回一个ResultSet实例,为结果集
			/*ResultSet的作用:1.first()指针移到第一行;结果集为空返回false,否则true
	                           2.last()指针移到第一行;结果集为空返回false,否则true
			                   3.previous()指针移到上一行;若存在上一行true,否则false
			                   4.next()指针移到下一行;若存在下一行true,否则false
			                   5.getRow()返回int类型的索引,从1开始
			*/
			System.out.println("索引 学号 姓名 成绩");
			while(rs.next())
			{
				int a=rs.getRow();
				int b=rs.getInt("xh");
				String c=rs.getString("name");
				int d=rs.getInt("cj");
				System.out.println(a+"   "+b+"   "+c+"   "+d);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//关闭资源
			if(rs!=null){
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			
			if(pst!=null){
				try {
					pst.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}	
		}	
	}
}

Guess you like

Origin blog.csdn.net/weixin_59798969/article/details/124046725