Java 小白 jabc增删改查(未优化)

package com.wj.demo01;

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

import org.junit.Test;

/**
 * 增删改查操作
 * @author 开心
 *
 */
public class Demo02 {
	
	@Test
	public void query() {
		//注册驱动
		//获取连接
		//创建发送sql语句的对象
		//执行sql语句
		//处理结果集
		//关闭资源
		Connection conn=null;
		Statement stmt=null;
		ResultSet rs=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://localhost:3306/mydb3";
			String user="root";
			String password="1234";
			conn=DriverManager.getConnection(url, user, password);
			
			stmt=conn.createStatement();
			rs=stmt.executeQuery("select * from student");
			while(rs.next()) {
				int id=rs.getInt("id");
				String name=rs.getString("name");
				String city=rs.getString("city");
				int age=rs.getInt("age");
				System.out.println(id+":"+name+":"+city+":"+age);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(null!=rs) {
					rs.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			try {
				if(null!=stmt) {
					stmt.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			try {
				if(null!=conn) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	
	
	
	
	
	
	@Test
	public void delete() {
		//注册驱动
		//获得连接
		//获取发送sql语句的对象
		//执行sql语句
		//如果有结果集就处理结果集
		//关闭资源
		Connection conn=null;
		Statement stmt=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb3","root", "1234");
			stmt=conn.createStatement();
			String sql="delete from student where id=11";
			
			int count=stmt.executeUpdate(sql);
			System.out.println("删除的行数是"+count);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(null!=stmt) {
					stmt.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	
	
	
	
	
	
	
	//测试JDBC的修改操作
	@Test
	public void update() {
		//注册驱动
		//获得连接
		//创建发送sql语句的对象
		//执行sql语句,
		//如果有结果集就处理结果集
		//关闭资源
		Connection conn=null;
		Statement stmt=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://localhost:3306/mydb3";
			String user="root";
			String password="1234";
			conn=DriverManager.getConnection(url, user, password);
			stmt=conn.createStatement();
			String sql="update student set name='开心',city='广州' where id=11 ";
			int count=stmt.executeUpdate(sql);
			System.out.println("受影响的行数"+count);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(stmt!=null) {
					stmt.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			try {
				if(conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	
	//添加操作
	@Test
	public void insert() {
		Connection conn=null;
		Statement stmt=null;
		try {
			//注册驱动
			Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://localhost:3306/mydb3";
			String user="root";
			String password="1234";
			conn=DriverManager.getConnection(url,user,password);
			//获取发送sql语句的对象
			stmt=conn.createStatement();
			String sql="insert into student values(null,'开心','广州',18)";
			//执行sql语句
			int count=stmt.executeUpdate(sql);
			System.out.println("受影响的行数是"+count);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(stmt!=null) {
					stmt.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			
		}
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值