jdbc的连接基本方法和实现对数据库中的数据增删查改

    技术2022-07-11  70

    1.java程序通过jdbc连接数据库的方法简介,代码模块:

    // # Java数据库连接基本步骤 public void connection () throws SQLException { try { // #1 j加载mysql的Driver驱动(导包,从file,工程下导入,参考log4j) Class.forName("com.mysql.jdbc.Driver"); //#2 与数据库建立连接 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/compony?useUnicode=true&characterEncoding=utf-8", "root", "root"); //#3 创建状态 st = con.createStatement(); //#4 得到结果集 rs = st.executeQuery("select deptno,dname,loc from dept"); //#5 处理结果,根据上方的sql语句运行结果进行处理 while (rs.next()) { int deptno = rs.getInt("deptno"); String dname = rs.getNString("dname"); String loc = rs.getNString("loc"); System.out.println(deptno + "\t" + dname + "\t" + loc); } } catch (Exception e) { e.printStackTrace(); } //#6 释放资源(顺序<从后往前>关闭连接) finally { rs.close(); st.close(); con.close(); } } public static void main (String[]args){ ConnectionTest ct = new ConnectionTest(); try { ct.connection(); } catch (SQLException e) { e.printStackTrace(); } }

    2.通过创建表的封装类,操作类,连接类,测试类来对封装类的表数据进行增删改查,代码模块:

    1:创建表的封装类,封装需要操作的表的属性;代码模块:(省略get,set方法,toString方法,有参无参构造方法,只是作为演示) public class dept { //# dept 表的封装类,用于数据操作 private int deptno; // 部门编号,主键 private String dname; //部门名称 private String loc; //地址 } 2:创建连接类,编写数据库的连接方法和释放资源方法,供操作类调用; public class ConnectionTest { Connection con=null; Statement st=null; ResultSet rs=null; //#1 创建数据库连接(getconnection)方法,返回值为一个连接 public Connection getConnection() throws Exception { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/compony?useUnicode=true&characterEncoding=utf-8","root","root"); return con; } //#2 创建关闭资源的方法 //# 因为参数只有连接和声明,所以适用于增删改操作的关闭资源 public void close(Statement st,Connection con) throws SQLException { if (st!=null){ st.close(); } if (con!=null){ con.close(); } } //#因为参数只有连接,声明和结果集,所以适用于查询操作的关闭资源 public void close(ResultSet rs,Statement st,Connection con) throws SQLException { if (rs != null) { rs.close(); } if (st != null) { st.close(); } if (con != null) { con.close(); } } } 3.创建操作类,对表数据进行操作(增删改查)代码模块: public class Operation { Connection con=null; Statement st=null; ResultSet rs=null; List <dept> list=new ArrayList<>(); ConnectionTest ct=new ConnectionTest(); //# 增 public int insert(int deptno,String dname,String loc) throws Exception { con=ct.getConnection(); st=con.createStatement(); int i=st.executeUpdate("insert into dept(deptno,dname,loc) values("+deptno+",'"+dname+"','"+loc+"')"); return i; } //# 删 public int delete(int deptno) throws Exception { con=ct.getConnection(); st=con.createStatement(); int i=st.executeUpdate("delete from dept where deptno="+deptno); return i; } //# 改 public int update(int deptno,String dname,String loc) throws Exception { con=ct.getConnection(); st=con.createStatement(); int i=st.executeUpdate("update dept set dname='"+dname+"',loc='"+loc+"' where deptno="+deptno); return i; } //# 查 public List<dept> select() throws Exception { con=ct.getConnection(); st=con.createStatement(); rs=st.executeQuery("select deptno,dname,loc from dept"); while (rs.next()){ dept d=new dept(); d.setDeptno(rs.getInt("deptno")); d.setDname(rs.getNString("dname")); d.setLoc(rs.getNString("loc")); list.add(d); } ct.close(rs,st,con); return list; } } 4.创建测试类,调用操作类的操作方法并传入参数,进行对表数据的操作,代码模块: public class Test { Operation op=new Operation(); public void insert(){ try { int j= op.insert(33,"青龙众","伏龙山"); if (j==1){ System.out.println("添加成功"); }else if(j==0){ System.out.println("添加失败"); } List <dept> list=op.select(); for (dept de:list){ System.out.println(de.getDeptno()+"\t"+de.getDname()+"\t"+de.getLoc()); } } catch (Exception e) { e.printStackTrace(); } } public void delete() throws Exception { int j=op.delete(30); if (j==1){ System.out.println("删除成功"); }else if(j==0){ System.out.println("删除失败"); } List <dept> list=op.select(); for (dept de:list){ System.out.println(de.getDeptno()+"\t"+de.getDname()+"\t"+de.getLoc()); } } public void update() throws Exception { int j=op.update(3,"狮驼岭","武道山"); if (j==1){ System.out.println("修改成功"); }else if(j==0){ System.out.println("修改失败"); } List <dept> list=op.select(); for (dept de:list){ System.out.println(de.getDeptno()+"\t"+de.getDname()+"\t"+de.getLoc()); } } public void select() throws Exception { List <dept> list=op.select(); for (dept de:list){ System.out.println(de.getDeptno()+"\t"+de.getDname()+"\t"+de.getLoc()); } } public static void main(String[] args) throws Exception { Test t=new Test(); t.insert(); t.delete(); t.update(); t.select(); } }

    以上就是java程序通过jdbc和数据库建立连接,并操作数据库中的数据简单方法(增删改查);

    2020年7月1日

    Processed: 0.016, SQL: 10