Java使用Statement接口执行SQL语句操作实例分析
本文实例讲述了Java使用Statement接口执行SQL语句操作的方法。分享给大家供大家参考,具体如下:
Statement执行SQL语句:
1.对数据库的曾删改操作时,使用stmt.executeUpdate(sql) 执行给定SQL语句,分别为insert、update、delete.
2.对数据库做查询时,直接使用stmt.executeQuery(sql),返回结果可以为一个resultSet结果集。
首先做一些准备工作:
①对要进行操作的数据库表进行封装,比如说我的数据mydata中的aistu表,用AiMember.java进行封装,以便后面操作。具体如下:
packagecom.mysqltest.jdbc.model; /** *定义一个model *成员模型 *@authorAI_STU * */ publicclassAiMember{ privateStringname; privateintid; privateintage; privateStringemail; privateStringtel; privatedoublesalary; privateStringriqi; /** *alt+shift+s添加构造函数generatingconstructorusingfields. *@paramname *@paramid *@paramage *@paramemail *@paramtel *@paramsalary *@paramriqi */ publicAiMember(Stringname,intid,intage,Stringemail,Stringtel,doublesalary,Stringriqi){ super(); this.name=name; this.id=id; this.age=age; this.email=email; this.tel=tel; this.salary=salary; this.riqi=riqi; } //重构 publicAiMember(intid){ super(); this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicintgetId(){ returnid; } publicvoidsetId(intid){ this.id=id; } publicintgetAge(){ returnage; } publicvoidsetAge(intage){ this.age=age; } publicStringgetEmail(){ returnemail; } publicvoidsetEmail(Stringemail){ this.email=email; } publicStringgetTel(){ returntel; } publicvoidsetTel(Stringtel){ this.tel=tel; } publicdoublegetSalary(){ returnsalary; } publicvoidsetSalary(doublesalary){ this.salary=salary; } publicStringgetRiqi(){ returnriqi; } publicvoidsetRiqi(Stringriqi){ this.riqi=riqi; } }
②对连接MySQL数据库,和关闭连接方法进行封装,这里用DbUtil.java进行封装,具体如下:
packagecom.mysqltest.jdbc.modelComp; publicclassCompMember{ privateintid; privateStringname; privateintage; privatedoublesalary; /** *构造函数1 *@paramname *@paramage *@paramsalary */ publicCompMember(Stringname,intage,doublesalary){ super(); this.name=name; this.age=age; this.salary=salary; } /** *重载构造函数 *@paramid *@paramname *@paramage *@paramsalary */ publicCompMember(intid,Stringname,intage,doublesalary){ super(); this.id=id; this.name=name; this.age=age; this.salary=salary; } /** *get,set方法 */ publicintgetId(){ returnid; } publicvoidsetId(intid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicintgetAge(){ returnage; } publicvoidsetAge(intage){ this.age=age; } publicdoublegetSalary(){ returnsalary; } publicvoidsetSalary(doublesalary){ this.salary=salary; } @Override /** *改写toString,使得显示更好 */ publicStringtoString(){ return"["+this.id+"]"+this.name+","+this.age+","+this.salary; } }
准备工作做好了,下面开始使用Statement接口执行sql语句来实现增删改:
①增:
packagecom.mysqltest.jdbc.two2; importjava.sql.Connection; importjava.sql.Statement; importcom.mysqltest.jdbc.model.AiMember; importcom.mysqltest.jdbc.util.DbUtil; publicclassDemo3{ /** *添加成员到表中1 *@paramname *@paramid *@paramage *@paramemail *@paramtel *@paramsalary *@paramriqi *@return *@throwsException */ @SuppressWarnings("unused") privatestaticintaddMember(Stringname,intid,intage,Stringemail,Stringtel,doublesalary,Stringriqi)throwsException{ DbUtildbUtil=newDbUtil();//之前封装好的 Connectioncon=dbUtil.getCon();//获取数据库连接 Stringsql="insertintoaistuvalues('"+name+"',"+id+",'"+age+"','"+email+"','"+tel+"','"+salary+"','"+riqi+"')"; Statementstmt=con.createStatement();//获取statement intresult=stmt.executeUpdate(sql); dbUtil.close(stmt,con); returnresult; } /** *添加成员到表中2方法 *@parammem *@return *@throwsException */ privatestaticintaddMember2(AiMembermem)throwsException{//AiMember也是之前封装好的 //mem.getName(); DbUtildbUtil=newDbUtil();//之前封装好的 Connectioncon=dbUtil.getCon();//获取数据库连接 Stringsql="insertintoaistuvalues('"+mem.getName()+"',"+mem.getId()+",'"+mem.getAge()+"','"+mem.getEmail()+"','"+mem.getTel()+"','"+mem.getSalary()+"','"+mem.getRiqi()+"')"; Statementstmt=con.createStatement();//获取statement intresult=stmt.executeUpdate(sql); dbUtil.close(stmt,con); returnresult; } //privatestaticintaddMenber2() publicstaticvoidmain(String[]args)throwsException{ /*intresult=addMember("刘翔",4,28,"15xliu@stu.edu.cn","13411957776",8000.00,"2015-09-10"); if(result==1){ System.out.println("添加成功"); }else{ System.out.println("添加失败"); }*///多行注释,ctrl+shift+/ AiMembermem=newAiMember("李娜",6,25,"15nli@stu.edu.cn","13411957775",8000.00,"2015-09-03"); intresult=addMember2(mem); if(result==1){ System.out.println("添加成功"); }else{ System.out.println("添加失败"); } } }
②改:
packagecom.mysqltest.jdbc.two3; importjava.sql.Connection; importjava.sql.Statement; importcom.mysqltest.jdbc.model.AiMember; importcom.mysqltest.jdbc.util.DbUtil; publicclassDemo4{ privatestaticDbUtildbUtil=newDbUtil(); //@SuppressWarnings("unused") /** *修改成员 *@parammem *@return *@throwsException */ privatestaticintupdateMember(AiMembermem)throwsException{ Connectioncon=dbUtil.getCon();//获取数据库连接 Stringsql="updateaistusetname='"+mem.getName()+"',id="+mem.getId()+",age='"+mem.getAge() +"',email='"+mem.getEmail()+"',tel='"+mem.getTel()+"',salary='"+mem.getSalary()+"',riqi='" +mem.getRiqi()+"'whereid="+mem.getId(); //格式化,ctrl+a全选,然后ctrl+shift+f格式化 Statementstmt=con.createStatement();//获取statement intresult=stmt.executeUpdate(sql); dbUtil.close(stmt,con); returnresult; //return0; } publicstaticvoidmain(String[]args)throwsException{ AiMembermem=newAiMember("劳尔",6,24,"14elao@stu.edu.cn","13411957770",18000.00,"2014-09-03"); intresult=updateMember(mem); if(result==1){ System.out.println("更新成功"); }else{ System.out.println("更新失败"); } } }
③删:
packagecom.mysqltest.jdbc.two4; importjava.sql.Connection; importjava.sql.Statement; importcom.mysqltest.jdbc.model.AiMember; importcom.mysqltest.jdbc.util.DbUtil; publicclassDemo5{ privatestaticDbUtildbUtil=newDbUtil(); publicstaticintdeletMember(AiMembermem)throwsException{ Connectioncon=dbUtil.getCon();//获取数据库连接 Stringsql="deletefromaistuwhereid="+mem.getId(); Statementstmt=con.createStatement();//获取statement intresult=stmt.executeUpdate(sql); dbUtil.close(stmt,con); returnresult; } publicstaticvoidmain(String[]args)throwsException{ AiMembermem=newAiMember(5); intresult=deletMember(mem); if(result==1){ System.out.println("成功删除成员"); }else{ System.out.println("删除成员失败"); } } }
更多关于java相关内容感兴趣的读者可查看本站专题:《Java+MySQL数据库程序设计总结》、《Java数据结构与算法教程》、《Java文件与目录操作技巧汇总》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。