报告_实验九 通过JDBC方式操作数据库
- 格式:docx
- 大小:203.98 KB
- 文档页数:14
实验九通过JDBC方式操作数据库1.实验目的(1)掌握通过JDBC方式操作数据库的基本步骤。
(2)掌握增、删、改记录的方法。
(3)掌握查询记录以及遍历查询结果的方法。
2.实验内容实验题1学生信息管理函数。
数据库中的信息参考Exp9.1.txt,这些命令用来创建student表,包含学生的学号、姓名、年龄信息。
①根据学号,可以查询到学生的姓名和年龄;②给定学生的学号、姓名、年龄,在表中追加一行信息;③给定学生的学号,可以从表中删除该学生的信息;[基本要求] 对上面的每一个功能编写相应的函数,并测试。
运行结果:源代码:importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importjava.util.Scanner;importcom.mysql.jdbc.PreparedStatement;class Query{Scanner reader=new Scanner(System.in);Connection con;java.sql.PreparedStatementsql;public Query(){try{Class.forName("com.mysql.jdbc.Driver");}catch(ClassNotFoundException e){System.out.println(e);}}public void select(){String SNO;System.out.println("请输入要查询的学号:");SNO=reader.next();String URL="jdbc:mysql://localhost:3306/mysql";String user="root";String password="";ResultSet RS;try {con=DriverManager.getConnection(URL, user, password);} catch (SQLException e) {System.out.println(e);}try {sql=con.prepareStatement("select * from student where Sno=?");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 (SQLException e) {System.out.println(e);}}public void insert(){String SNO,SNAME,AGE;System.out.println("请输入要插入的信息:");SNO=reader.next();SNAME=reader.next();AGE=reader.next();StringURL="jdbc:mysql://localhost:3306/mysql?Unicode=true&characterEncoding=GBK";String user="root";String password="";ResultSet RS;try {con=DriverManager.getConnection(URL, user, password);} catch (SQLException e) {System.out.println(e);}try {sql=con.prepareStatement("insert into student values(?,?,?)");sql.setString(1, SNO);sql.setString(2, SNAME);sql.setString(3, AGE);int m=sql.executeUpdate();if(m!=0){System.out.println("添加成功!运行结果:");}else System.out.println("添加失败!运行结果:");/*查询插入后结果*/sql=con.prepareStatement("select * from student");RS=sql.executeQuery();while(RS.next()){System.out.println(RS.getString(1)+'\t'+RS.getString(2)+'\t'+RS.getString(3 ));}con.close();} catch (Exception e) {System.out.println(e);}}public void delete(){String SNO;System.out.println("请输入要删除信息的学号:");SNO=reader.next();String URL="jdbc:mysql://localhost:3306/mysql";String user="root";String password="";ResultSet RS;try {con=DriverManager.getConnection(URL, user, password);} catch (SQLException e) {System.out.println(e);}try {sql=con.prepareStatement("delete from student where Sno=?");sql.setString(1, SNO);int m=sql.executeUpdate();if(m!=0){System.out.println("删除成功!运行结果:");}else System.out.println("删除失败!运行结果:");/*查询删除后结果*/sql=con.prepareStatement("select * from student");RS=sql.executeQuery();while(RS.next()){System.out.println(RS.getString(1)+'\t'+RS.getString(2)+'\t'+RS.getString(3 ));}con.close();} catch (SQLException e) {System.out.println(e);}}}public class Experiment_9_JDBC {public static void main(final String[] args) {Query query=new Query();query.select();//查询query.insert();//插入query.delete();//删除}}实验题2学生信息管理系统。
图3.10 学生信息管理系统界面①点击“追加”按钮,可以追加一个学生信息;②点击“查询”按钮和“删除”按钮,可以按照学号查询或者删除一个学生信息;[技术提示]可以使用上一题开发的函数。
运行结果:添加记录成功后,再次查询,结果如下:然后按学号删除:检验是否删除成功:可知是删除成功的。
代码参考:package JDBC_Experiment;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Scanner;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Shell;import bel;import org.eclipse.swt.SWT;import org.eclipse.wb.swt.SWTResourceManager;import org.eclipse.swt.widgets.Text;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent;class Query{Scanner reader=new Scanner(System.in);Connection con;java.sql.PreparedStatement sql;public Query(){try{Class.forName("com.mysql.jdbc.Driver");}catch(ClassNotFoundException e){System.out.println(e);}}publicvoid select(Text text1,Text text2,Text text3,Text text4){ text2.setText("");text3.setText("");text4.setText("");text4.setText("请输入要查询的学号");String SNO=text1.getText();String URL="jdbc:mysql://localhost:3306/mysql";String user="root";String password="";ResultSet RS;try {con=DriverManager.getConnection(URL, user, password);} catch (SQLException e) {System.out.println(e);}try {sql=con.prepareStatement("select * from student where Sno=?");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 (SQLException e) {System.out.println(e);}}publicvoid insert(Text text1,Text text2,Text text3,Text text4){ text4.setText("");String SNO,SNAME,AGE;text4.setText("请输入要插入的信息");SNO=text1.getText();SNAME=text2.getText();AGE=text3.getText();StringURL="jdbc:mysql://localhost:3306/mysql?Unicode=true&characterEncoding=GBK";String user="root";String password="";ResultSetRS;try {con=DriverManager.getConnection(URL, user, password);} catch (SQLException e) {System.out.println(e);}try {sql=con.prepareStatement("insert into student values(?,?,?)");sql.setString(1, SNO);sql.setString(2, SNAME);sql.setString(3, AGE);int m=sql.executeUpdate();if(m!=0){text4.setText("添加成功!");}else text4.setText("添加失败!");con.close();} catch (Exception e) {System.out.println(e);}}publicvoid delete(Text text1,Text text2,Text text3,Text text4){ text2.setText("");text3.setText("");text4.setText("");String SNO;text4.setText("请输入要删除信息的学号:");SNO=text1.getText();String URL="jdbc:mysql://localhost:3306/mysql";String user="root";String password="";ResultSetRS;try {con=DriverManager.getConnection(URL, user, password);} catch (SQLException e) {System.out.println(e);}try {sql=con.prepareStatement("delete from student where Sno=?");sql.setString(1, SNO);int m=sql.executeUpdate();if(m!=0){text4.setText("删除成功!");}else text4.setText("删除失败!");con.close();} catch (SQLException e) {System.out.println(e);}}}publicclass JDBC_UI {protected Shell shell;private Text text;private Text text_1;private Text text_2;private Text text_3;/*** Launch the application.* @param args*/publicstaticvoid main(String[] args) {try {JDBC_UI window = new JDBC_UI();window.open();} catch (Exception e) {e.printStackTrace();}}/*** Open the window.*/publicvoid open() {Display display = Display.getDefault();createContents();shell.open();yout();while (!shell.isDisposed()) {if (!display.readAndDispatch()) {display.sleep();}}}/*** Create contents of the window.*/protectedvoid createContents() {final Query query=new Query();shell = new Shell();shell.setSize(450, 300);shell.setText("\u5B66\u751F\u4FE1\u606F\u7BA1\u7406\u7CFB\u7EDF");Label lblNewLabel = new Label(shell, SWT.NONE);lblNewLabel.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));lblNewLabel.setBounds(26, 42, 48, 21);lblNewLabel.setText("\u5B66\u53F7\uFF1A");Label lblNewLabel_1 = new Label(shell, SWT.NONE);lblNewLabel_1.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));lblNewLabel_1.setBounds(26, 78, 48, 21);lblNewLabel_1.setText("\u59D3\u540D\uFF1A");Label lblNewLabel_2 = new Label(shell, SWT.NONE);lblNewLabel_2.setFont(SWTResourceManager.getFont("微软雅黑", 12, SWT.NORMAL));lblNewLabel_2.setBounds(26, 116, 48, 21);lblNewLabel_2.setText("\u5E74\u9F84\uFF1A");Label lblNewLabel_3 = new Label(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 = new Text(shell, SWT.BORDER);text.setBounds(80, 42, 104, 23);text_1 = new Text(shell, SWT.BORDER);text_1.setBounds(80, 78, 104, 23);text_2 = new Text(shell, SWT.BORDER);text_2.setBounds(80, 114, 104, 23);text_3 = new Text(shell, SWT.BORDER);text_3.setEditable(false);text_3.setBounds(80, 177, 274, 23);Button button = new Button(shell, SWT.NONE);button.addSelectionListener(new SelectionAdapter() { @Overridepublicvoid widgetSelected(SelectionEvent e) {query.insert(text, text_1, text_2, text_3);}});button.setBounds(274, 42, 80, 27);button.setText("\u8FFD\u52A0");Button button_1 = new Button(shell, SWT.NONE);button_1.addSelectionListener(new SelectionAdapter() { @Overridepublicvoid widgetSelected(SelectionEvent e) {query.select(text, text_1, text_2, text_3);}});button_1.setBounds(274, 78, 80, 27);button_1.setText("\u67E5\u8BE2");Button button_2 = new Button(shell, SWT.NONE);button_2.addSelectionListener(new SelectionAdapter() { @Overridepublicvoid widgetSelected(SelectionEvent e) {query.delete(text, text_1, text_2, text_3);}});button_2.setBounds(274, 116, 80, 27);button_2.setText("\u5220\u9664");}}。