jdbc-对数据库进行增删改查
一、JDBC组件:
- pojo 组件:创建数据库实体类
- dao组件数据访问对象;dao处理组件一般分为四个:定义添加数据方法、定义更新数据方法、定义删除数据方法、定义各种业务查询方法
- util数据库访问工具类
二、properties:配置文件信息
##数据库配置信息 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/xzit jdbc.user=root jdbc.password=root /** *jdbc对数据库进行增删改查操作时:需要对url进行修改!!!!!!!!!(很重要告诉jdbc访问的数据库名称) *未修改:jdbc.url=jdbc:mysql://localhost:3306 *报错:No database selected * *修改后:jdbc.url=jdbc:mysql://localhost:3306/xzit(斜杠加上要操作的数据名字) *通过 **/
三、JDBC中dao组件
- 对数据库进行:DML(insert、delete、udate)增删改查操作:
- inser插入:增
/** * 数据访问DAO组件实现对数据表Province 的DML(inser、update、delete)操作 */ public class AreaDao { /** * 添加新省份区域 * @param province * @return */ public int addArea ( Province province ) { int res = 0; Connection conn = null; PreparedStatement ps = null; try { //插入sql(DML)语句到数据库xzit,province表格中 String sql = "insert into province(id,name,municipality,description)" + " values (?,?,?,?)"; //获取数据连接 conn = JDBCUtil.getConnection(); //获取数据库操作对象 ps = conn.prepareStatement(sql); ps.setString(1 , province.getId()); ps.setString(2 , province.getName()); ps.setString(3 , province.getMunicipality()); ps.setString(4 , province.getDescription()); //执行sql语句 res = ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { //释放资源 JDBCUtil.close(ps); JDBCUtil.close(conn); } return res; } }
//编写测试insert插入测试
public class TestInsert { public static void main ( String[] args ) { Province province = new Province(); province.setId("6"); province.setName("上海"); province.setMunicipality("1"); province.setDescription("直辖市"); int res = new AreaDao().addArea(province); System.out.println("影响是数据库表的行数:"+res); } }
- 修改数据库数据:
public class AreaDao {
/** * 修改数据库xzit 中的Province 表格数据 * @param province * @return */ public int modifyProvince ( Province province ) { int res = 0; Connection conn = null; PreparedStatement ps = null; try { //获取数据库连接 conn = JDBCUtil.getConnection(); String sql = "update province set name=?,municipality=?,description=?" + "where id = ?"; //获取数据库操作对象 ps = conn.prepareStatement(sql); //执行sql ps.setString(1 , province.getId()); ps.setString(2 , province.getName()); ps.setString(3 , province.getMunicipality()); ps.setString(4 , province.getDescription()); //执行sql语句 res = ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtil.close(conn); JDBCUtil.close(ps); } return res; }
}
//测试修改组件
public class TestUpdate { public static void main ( String[] args ) { Province province =new Province(); province.setId("1"); province.setName("北京"); province.setMunicipality("1"); province.setDescription("新修改数据"); int res = new AreaDao().modifyProvince(province); System.out.println("新修改id为1的数据是:"+res); } }
- 删除数据:
public class AreaDao {
/** * 对数据库xzit进行删除 * @param id * @return */ public int deleteProvinceById ( String id ) { int res = 0; Connection conn = null; PreparedStatement ps = null; try { //获取链接 conn = JDBCUtil.getConnection(); String sql = "delete from province where id ='" + id + "'"; //获取数据库操作对象 ps = conn.prepareStatement(sql); //执行sql ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { //关闭资源 JDBCUtil.close(conn); JDBCUtil.close(ps); } return res; }
}
//删除数据
public class TestDeletet { public static void main ( String[] args ) { String id = "1"; int res = 0; res = new AreaDao().deleteProvinceById(id); System.out.println("删除数据条数:"+res); } }
- 查询数据xzit;province表中所有数据
/** * 查询获取表中所有省份信息 * @return */ public List<Province> queryProvinceList () { List<Province> provinceList = new ArrayList<Province>(); String sql = "select id,name,municipality,description from province"; Connection conn = null; PreparedStatement ps = null; ResultSet set = null; try { //获取数据库链接 conn = JDBCUtil.getConnection(); //获取数据库操作对象 ps = conn.prepareStatement(sql); //查询返回ResultSet结果集对象 set = ps.executeQuery(); while (set.next()) { //每一行创建一个Province对象 Province province = new Province(); String id = set.getString(1); province.setId(id); province.setName(set.getString("name")); province.setMunicipality(set.getString("Municipality")); province.setDescription(set.getString("DESCRIPTION")); //添加province对象到list集合中 provinceList.add(province); } } catch (Exception e) { e.printStackTrace(); } finally { //关闭资源 JDBCUtil.close(set); JDBCUtil.close(ps); JDBCUtil.close(conn); } return provinceList; } }
//查询数据库xzit,province表中所有数据
public class TestBaseQuery { public static void main ( String[] args ) { List<Province> provinces = new AreaDao().queryProvinceList(); System.out.println("ID\t省份名称\t直辖市(1是,0不是)\t描述"); for (Province province : provinces) { System.out.println(province.getId() +"\t"+province.getName() +"\t"+province.getMunicipality() +"\t"+province.getDescription()); } } }
四、JDBC中pojo组件
- 创建访问数据库的实体类,对外提供set、get方法
package com.xzit.platfrom.task.pojo; public class Province { private String id; private String name; private String municipality; private String Description; public String getId () { return id; } public void setId ( String id ) { this.id = id; } public String getName () { return name; } public void setName ( String name ) { this.name = name; } public String getMunicipality () { return municipality; } public void setMunicipality ( String municipality ) { this.municipality = municipality; } public String getDescription () { return Description; } public void setDescription ( String description ) { Description = description; } }