5.CRUD操作
发布日期:2021-05-06 18:58:18 浏览次数:27 分类:精选文章

本文共 3271 字,大约阅读时间需要 10 分钟。

1.增加

package cn.edu.hbue.wmp;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;public class GeneralJDBCInsert {   		public static void main(String args[]){   						Student stu = new Student(14,"小汤","18462@edu","2000-4-1");		String sql = "insert into student values(?,?,?,?)";		isnert(sql,stu);			}		public static void isnert(String sql,Student stu){     //插入一条学生记录				PreparedStatement ps = null;		Connection connection = null;						//1.获取连接		connection = JDBCUtil.getConn();				try {   			//2.预编译sql			ps = connection.prepareStatement(sql);									//3.填充占位符			ps.setInt(1, stu.getId());			ps.setString(2, stu.getName());			ps.setString(3, stu.getEmail());			ps.setString(4, stu.getBirth());									//4.执行操作			ps.execute();		} catch (SQLException e) {   			// TODO Auto-generated catch block			e.printStackTrace();		}finally{   						//5.关闭资源			JDBCUtil.closeConn(ps, connection);		}					}}

2.删除

package cn.edu.hbue.wmp;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;public class GeneralJDBCUpdate {   	public static void main(String[] args) {   		// TODO Auto-generated method stub				String sql = "delete from student where id = ?";		update(sql,11);	}		//要传入sql语句和不确定的占位符	public static void update(String sql,Object ... args){   				Connection connection = null;		PreparedStatement ps = null;		//1.获取连接		connection = JDBCUtil.getConn();				try {   			//2.预编译sql语句			ps = connection.prepareStatement(sql);						//3.填充占位符			for(int i = 0;i < args.length;i++){   				ps.setObject(i + 1, args[i]);			}						//4.执行sql语句			ps.execute();		} catch (SQLException e) {   			// TODO Auto-generated catch block			e.printStackTrace();		}finally{   						//5.关闭资源			JDBCUtil.closeConn(ps,connection);					}		}}

3.修改

package cn.edu.hbue.wmp;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;public class GeneralJDBCAlter {   	public static void main(String[] args) {   		// TODO Auto-generated method stub				String sql = "update student set name = ? where id = ?";		alter(sql,"汤汤",14);			}		public static void alter(String sql,Object...args)	{   				PreparedStatement ps = null;		Connection connection = null;						//1.获取连接		connection = JDBCUtil.getConn();				try {   			//2.预编译sql语句			ps = connection.prepareStatement(sql);						//3.填充占位符			for(int i = 0;i

4.查询(最难)

在查询中如果数据库字段名和java类中属性名不一致,就在sql语句中起别名,然后在后面操作中

// 不获取列名获取列别名,不用rsmd.getColumnName(i+1)(获取列名)
String columnName = rsmd.getColumnLabel(i + 1); // 最好不管有没有不同都用这个

package cn.edu.hbue.wmp;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import com.mysql.cj.jdbc.result.ResultSetMetaData;import com.mysql.cj.result.Field;public class GeneralJDBCQuery {   	public static void main(String[] args) {   			String sql = "select id,name,email from student where id = ?";		Student stu = query(sql,12);		System.out.println(stu);		}		public static Student query(String sql,Object...args){   				Connection connection = null;		ResultSet rs = null;		PreparedStatement ps = null;				//1获取连接		connection = JDBCUtil.getConn();												try {   				//2预编译SQL语句				ps = connection.prepareStatement(sql);												// 3.填充占位符				for(int i = 0;i
上一篇:java中获取程序运行时间
下一篇:4.JDBC与代码重构

发表评论

最新留言

表示我来过!
[***.240.166.169]2025年04月09日 23时41分04秒