【Java Web开发】关于数据库的操作

本文介绍如何使用 Java 进行 MySQL 数据库的基本操作,包括建立连接、增删查改等常见操作,并提供了具体的代码实现。

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

1.mysql连接数据库并创建数据库连接对象

第一种:

Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaweb?user=root&password=welcom123");

第二种:

public class DBConnection {
	
	private static String url="jdbc:mysql://localhost:3306/javaweb";
	private static String user = "root";
	private static String password="welcome123";
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/*
	 * 获取连接对象
	 */
	public static Connection getConn(){
		Connection conn = null;
		try{
			conn = (Connection) DriverManager.getConnection(url,user,password);
		}catch(SQLException e){
			e.printStackTrace();
		}
		return conn;
	}

2.对数据库进行增删查改

public static void insert(String sno,String name,String sex,int age){
		Connection conn = null;
		PreparedStatement ps=null;
		try {
			conn = DBConnection.getConn();
			String sql = "insert into students(no,name,sex,age) values(?,?,?,?)";
			ps =(PreparedStatement) conn.prepareStatement(sql);
			ps.setString(1,sno);
			ps.setString(2,name);			
			ps.setString(3, sex);
			ps.setInt(4, age);
			ps.executeUpdate();			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();			
		}finally{
			try {
				if(null!=ps){
					ps.close();
				}
				if(null!=conn){
					conn.close();
				}
				
			} catch (SQLException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}
public static void delById(int id){
		Connection conn = null;
		String sql = "delete from students where id = ?";
		PreparedStatement ps = null;
		try {
			conn = DBConnection.getConn();
			ps =(PreparedStatement) conn.prepareStatement(sql);
			ps.setInt(1, id);
			ps.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{			
				try {
					if(null !=ps){
						ps.close();
					}
					if(null !=conn){
						conn.close();
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
		}
		
		
	}

第一种:根据id查

public static Student queryById(int id){
		String sql = "select * from students where id = ?";
		Connection conn = null;
		PreparedStatement ps = null;	
		ResultSet res = null;
		Student student = null;
		try {
			conn = DBConnection.getConn();
			ps =(PreparedStatement) conn.prepareStatement(sql);
			ps.setInt(1, id);
			res = ps.executeQuery();			
			while(res.next()){
				student = new Student(res.getInt(1),res.getString(2), res.getString(3), res.getString(4), res.getInt(5));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try{
				if(null!=res){
					res.close();				
				}
				if(null!=ps){
					ps.close();
				}
				if(null!=conn){
					conn.close();
				}
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		return student;
	}

第二种:查询所有

public static List<Student> queryAll(){
		List<Student> list=new ArrayList<Student>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		try {
			 conn = DBConnection.getConn();
			 ps = (PreparedStatement)conn.prepareStatement("SELECT * from students");
			 res = ps.executeQuery();
			 while(res.next()){
				 list.add(new Student(res.getInt(1),res.getString(2),res.getString(3),res.getString(4),res.getInt(5)));
			 }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(null!=res){
					res.close();
				}
				if(null!=ps){
					ps.close();
				}
				if(null!=conn){
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}
public static void updateById(int id,String no,String name,String sex,int age){
		Connection conn = null;
		PreparedStatement ps = null;
		String sql = "UPDATE students SET no=?,name =?,sex=?,age=? WHERE id = ? ";
		try {
			conn = DBConnection.getConn();
			ps = (PreparedStatement)conn.prepareStatement(sql);
			ps.setString(1, no);
			ps.setString(2, name);
			ps.setString(3, sex);
			ps.setInt(4, age);
			ps.setInt(5, id);
			ps.executeUpdate();
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {				
				if(null!=ps){
					ps.close();
				}
				if(null!=conn){
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

数据库操作的其他方法
方法参数类型返回值类型适用位置
Statement.executeUpdate(sql)String,不带未知值不带返回的insert、update、delete操作
Statement.executeQuery(sql)String,不带未知值ResultSet知道参数的查询
Statement.getFetchSize()int,获取ResultSet的设置大小预先setFetchSize(n),可通过该方法获取
Statement.setMaxRows(max)int设置ResultSet最大包含行数
PreparedStatement.clearParameters()需要重复使用预处理时,清除当前参数值
Statement.execute(sql)String判断结果是否为ResultSet对象执行任何指定的sql语句

PreparedStatement.executeQuery()

ResultSet已经设置好预处理sql语句
PreparedStatement.executeUpdate()设置好预处理sql,处理无返回内容的sql语句
PreparedStatement.getMetaData()ResultSetMetaData 对象获取ResultSet对象的列描述



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值