java 小白 jdbc增删改查(优化)

本文针对Java初学者,详细介绍了使用JDBC进行数据库的增删改查操作,并分享了一些性能优化技巧,包括批处理、连接池的使用等,旨在提升数据库交互效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

查询代码
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 Demo3 {
	
	@Test
	public void query() {
		//注册驱动
		//获取连接
		//创建发送sql语句的对象
		//执行sql语句
		//处理结果集
		//关闭资源
		Connection conn=null;
		Statement stmt=null;
		ResultSet rs=null;
		try {
			conn=JDBCUtil.getConnection();
			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 (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.release(stmt, conn, rs);
		}
	}
	
	
	
	
	
	
	
	
	@Test
	public void delete() {
		//注册驱动
		//获得连接
		//获取发送sql语句的对象
		//执行sql语句
		//如果有结果集就处理结果集
		//关闭资源
		Connection conn=null;
		Statement stmt=null;
		try {
			conn=JDBCUtil.getConnection();
			stmt=conn.createStatement();
			String sql="delete from student where id=11";
			
			int count=stmt.executeUpdate(sql);
			System.out.println("删除的行数是"+count);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.release(stmt, conn);
		}
		
	}
	
	
	
	
	
	
	
	
	//测试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();
			}
			
			
		}
		
	}
}


工具包
package com.wj.demo01;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtil {
	private static String url=null;
	private static String user=null;
	private static String password=null;
	//src路径叫类加载路径,我们就是加载类加载生成Properties对象
	static {
		try {
			Properties prop=new Properties();
			//获取类加载路径下的文件
			InputStream is=JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties");
			prop.load(is);
			String driver=prop.getProperty("dirver");
			//
			Class.forName(driver);
			
			url=prop.getProperty("url");
			
			user=prop.getProperty("user");
			
			password=prop.getProperty("password");
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//获取连接对象
	public static Connection getConnection() throws SQLException {
		return DriverManager.getConnection(url, user, password);
	}
	
	public static void release(Statement stmt,Connection conn) {
		try {
			if(stmt!=null) {
				stmt.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if(conn!=null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void release(Statement stmt,Connection conn,ResultSet rs) {
		try {
			if(null!=rs) {
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		release(stmt, conn);
	}
}
在scr下创建的文件
dirver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb3
user=root
password=1234
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值