报告-实验九-通过JDBC方式操作数据库_第1页
报告-实验九-通过JDBC方式操作数据库_第2页
报告-实验九-通过JDBC方式操作数据库_第3页
报告-实验九-通过JDBC方式操作数据库_第4页
报告-实验九-通过JDBC方式操作数据库_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验九通过JDBC方式操作数据库1.实验目的〔1〕掌握通过JDBC方式操作数据库的根本步骤。〔2〕掌握增、删、改记录的方法。〔3〕掌握查询记录以及遍历查询结果的方法。2.实验内容实验题1学生信息管理函数。数据库中的信息参考Exp9.1.txt,这些命令用来创立student表,包含学生的学号、姓名、年龄信息。①根据学号,可以查询到学生的姓名和年龄;②给定学生的学号、姓名、年龄,在表中追加一行信息;③给定学生的学号,可以从表中删除该学生的信息;[根本要求]对上面的每一个功能编写相应的函数,并测试。运行结果:源代码:import;import;import;import;import;import;import;classQuery{ Scannerreader=newScanner(System.in); Connectioncon; sql; publicQuery(){ try{ Class.forName(""); } catch(ClassNotFoundExceptione){ (e); } } publicvoidselect(){ StringSNO; ("请输入要查询的学号:"); SNO=reader.next(); StringURL="jdbc:mysql://localhost:3306/mysql"; Stringuser="root"; Stringpassword=""; ResultSetRS; try{ con=DriverManager.getConnection(URL,user,password); }catch(SQLExceptione){ (e); } try{ sql=con.prepareStatement("select*fromstudentwhereSno=?"); sql.setString(1,SNO); RS=sql.executeQuery(); while(RS.next()){ System.out.println(RS.getString(1)+'\t'+RS.getString(2)+'\t'+RS.getString(3)); } con.close(); }catch(SQLExceptione){ (e); } } publicvoidinsert(){ StringSNO,SNAME,AGE; ("请输入要插入的信息:"); SNO=reader.next(); SNAME=reader.next(); AGE=reader.next(); StringURL="jdbc:mysql://localhost:3306/mysql?Unicode=true&characterEncoding=GBK"; Stringuser="root"; Stringpassword=""; ResultSetRS; try{ con=DriverManager.getConnection(URL,user,password); }catch(SQLExceptione){ (e); } try{ sql=con.prepareStatement("insertintostudentvalues(?,?,?)"); sql.setString(1,SNO); sql.setString(2,SNAME); sql.setString(3,AGE); intm=sql.executeUpdate(); if(m!=0){ ("添加成功!运行结果:"); } else("添加失败!运行结果:"); /*查询插入后结果*/ sql=con.prepareStatement("select*fromstudent"); RS=sql.executeQuery(); while(RS.next()){ System.out.println(RS.getString(1)+'\t'+RS.getString(2)+'\t'+RS.getString(3)); } con.close(); }catch(Exceptione){ (e); } } publicvoiddelete(){ StringSNO; ("请输入要删除信息的学号:"); SNO=reader.next(); StringURL="jdbc:mysql://localhost:3306/mysql"; Stringuser="root"; Stringpassword=""; ResultSetRS; try{ con=DriverManager.getConnection(URL,user,password); }catch(SQLExceptione){ (e); } try{ sql=con.prepareStatement("deletefromstudentwhereSno=?"); sql.setString(1,SNO); intm=sql.executeUpdate(); if(m!=0){ ("删除成功!运行结果:"); } else("删除失败!运行结果:"); /*查询删除后结果*/ sql=con.prepareStatement("select*fromstudent"); RS=sql.executeQuery(); while(RS.next()){ System.out.println(RS.getString(1)+'\t'+RS.getString(2)+'\t'+RS.getString(3)); } con.close(); }catch(SQLExceptione){ (e); } } }publicclassExperiment_9_JDBC{ publicstaticvoidmain(finalString[]args){ Queryquery=newQuery(); query.select();//查询 query.insert();//插入 query.delete();//删除 }}实验题2学生信息管理系统。 学号:学号:姓名:年龄:查询追加删除状态:图3.10学生信息管理系统界面①点击“追加”按钮,可以追加一个学生信息;②点击“查询”按钮和“删除”按钮,可以按照学号查询或者删除一个学生信息;[技术提示]可以使用上一题开发的函数。运行结果:添加记录成功后,再次查询,结果如下:然后按学号删除:检验是否删除成功:可知是删除成功的。代码参考:packageJDBC_Experiment;import;import;import;import;import;import;import;import;import;import;import;import;import;import;classQuery{ Scannerreader=newScanner(System.in); Connectioncon; sql; publicQuery(){ try{ Class.forName(""); } catch(ClassNotFoundExceptione){ System.out.println(e); } } publicvoidselect(Texttext1,Texttext2,Texttext3,Texttext4){ text2.setText(""); text3.setText(""); text4.setText(""); text4.setText("请输入要查询的学号"); StringSNO=text1.getText(); StringURL="jdbc:mysql://localhost:3306/mysql"; Stringuser="root"; Stringpassword=""; ResultSetRS; try{ con=DriverManager.getConnection(URL,user,password); }catch(SQLExceptione){ System.out.println(e); } try{ sql=con.prepareStatement("select*fromstudentwhereSno=?"); sql.setString(1,SNO); RS=sql.executeQuery(); if(RS.next()){ text2.setText(RS.getString(2)); text3.setText(RS.getString(3)); text4.setText("查询成功!"); } else{ text4.setText("查无此人!"); } con.close(); }catch(SQLExceptione){ System.out.println(e); } } publicvoidinsert(Texttext1,Texttext2,Texttext3,Texttext4){ text4.setText(""); StringSNO,SNAME,AGE; text4.setText("请输入要插入的信息"); SNO=text1.getText(); SNAME=text2.getText(); AGE=text3.getText(); StringURL="jdbc:mysql://localhost:3306/mysql?Unicode=true&characterEncoding=GBK"; Stringuser="root"; Stringpassword=""; ResultSetRS; try{ con=DriverManager.getConnection(URL,user,password); }catch(SQLExceptione){ System.out.println(e); } try{ sql=con.prepareStatement("insertintostudentvalues(?,?,?)"); sql.setString(1,SNO); sql.setString(2,SNAME); sql.setString(3,AGE); intm=sql.executeUpdate(); if(m!=0){ text4.setText("添加成功!"); } elsetext4.setText("添加失败!"); con.close(); }catch(Exceptione){ System.out.println(e); } } publicvoiddelete(Texttext1,Texttext2,Texttext3,Texttext4){ text2.setText(""); text3.setText(""); text4.setText(""); StringSNO; text4.setText("请输入要删除信息的学号:"); SNO=text1.getText(); StringURL="jdbc:mysql://localhost:3306/mysql"; Stringuser="root"; Stringpassword=""; ResultSetRS; try{ con=DriverManager.getConnection(URL,user,password); }catch(SQLExceptione){ System.out.println(e); } try{ sql=con.prepareStatement("deletefromstudentwhereSno=?"); sql.setString(1,SNO); intm=sql.executeUpdate(); if(m!=0){ text4.setText("删除成功!"); } elsetext4.setText("删除失败!"); con.close(); }catch(SQLExceptione){ System.out.println(e); } } }publicclassJDBC_UI{ protectedShellshell; privateTexttext; privateTexttext_1; privateTexttext_2; privateTexttext_3; /** *Launchtheapplication. *@paramargs */ publicstaticvoidmain(String[]args){ try{ JDBC_UIwindow=newJDBC_UI(); window.open(); }catch(Exceptione){ e.printStackTrace(); } } /** *Openthewindow. */ publicvoidopen(){ Displaydisplay=Display.getDefault(); createContents(); shell.open(); shell.layout(); while(!shell.isDisposed()){ if(!display.readAndDispatch()){ display.sleep(); } } } /** *Createcontentsofthewindow. */ protectedvoidcreateContents(){ finalQueryquery=newQuery(); shell=newShell(); shell.setSize(450,300); shell.setText("\u5B66\u751F\u4FE1\u606F\u7BA1\u7406\u7CFB\u7EDF"); LabellblNewLabel=newLabel(shell,SWT.NONE); lblNewLabel.setFont(SWTResourceManager.getFont("微软雅黑",12,SWT.NORMAL)); lblNewLabel.setBounds(26,42,48,21); lblNewLabel.setText("\u5B66\u53F7\uFF1A"); LabellblNewLabel_1=newLabel(shell,SWT.NONE); lblNewLabel_1.setFont(SWTResourceManager.getFont("微软雅黑",12,SWT.NORMAL)); lblNewLabel_1.setBounds(26,78,48,21); lblNewLabel_1.setText("\u59D3\u540D\uFF1A"); LabellblNewLabel_2=newLabel(shell,SWT.NONE); lblNewLabel_2.setFont(SWTResourceManager.getFont("微软雅黑",12,SWT.NORMAL)); lblNewLabel_2.setBounds(26,116,48,21); lblNewLabel_2.setText("\u5E74\u9F84\uFF1A"); LabellblNewLabel_3=newLabel(shell,SWT.NONE); lblNewLabel_3.setFont(SWTResourceManager.getFont("微软雅黑",12,SWT.NORMAL)); lblNewLabel_3.setBounds(26,179,48,21); lblNewLabel_3.setText("\u72B6\u6001\uFF1A"); text=newText(shell,SWT.BORDER); text.setBounds(80,42,104,23); text_1=newText(shell,SWT.BORDER); text_1.setBounds(80,78,104,23); text_2=newText(shell,SWT.BORDER); text_2.setBounds(80,114,104,23); text_3=newText(shell,SWT.BORDER); text_3.setEditable(false); text_3.setBounds(80,177,274,23); Buttonbutton=newButton(shell,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论