报告_实验九 通过jdbc方式操作数据库

上传人:第*** 文档编号:57515053 上传时间:2018-10-22 格式:PDF 页数:14 大小:229.94KB
返回 下载 相关 举报
报告_实验九 通过jdbc方式操作数据库_第1页
第1页 / 共14页
报告_实验九 通过jdbc方式操作数据库_第2页
第2页 / 共14页
报告_实验九 通过jdbc方式操作数据库_第3页
第3页 / 共14页
报告_实验九 通过jdbc方式操作数据库_第4页
第4页 / 共14页
报告_实验九 通过jdbc方式操作数据库_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《报告_实验九 通过jdbc方式操作数据库》由会员分享,可在线阅读,更多相关《报告_实验九 通过jdbc方式操作数据库(14页珍藏版)》请在金锄头文库上搜索。

1、实验九实验九 通过通过 JDBCJDBC 方式操作数据库方式操作数据库1 1实验目的实验目的(1)掌握通过 JDBC 方式操作数据库的基本步骤。(2)掌握增、删、改记录的方法。(3)掌握查询记录以及遍历查询结果的方法。2 2实验内容实验内容实验题实验题 1 1 学生信息管理函数。数据库中的信息参考 Exp9.1.txt,这些命令用来创建 student 表,包含学生的学号、姓名、年龄信息。 根据学号,可以查询到学生的姓名和年龄; 给定学生的学号、姓名、年龄,在表中追加一行信息; 给定学生的学号,可以从表中删除该学生的信息;基本要求 对上面的每一个功能编写相应的函数,并测试。运行结果:源代码:源

2、代码:import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Scanner;import com.mysql.jdbc.PreparedStatement;class QueryScanner reader=new Scanner(System.in);Connection con;java.sql.PreparedStatement sql

3、;public Query()tryClass.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=Driv

4、erManager.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();

5、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=trueString user=“root“;String password=“;ResultSet RS;try con=DriverManag

6、er.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

7、.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(“请输

8、入要删除信息的学号:“);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.se

9、tString(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(

10、); catch (SQLException e) System.out.println(e);public class Experiment_9_JDBC public static void main(final String args) Queryquery=new Query();query.select();/查询query.insert();/插入query.delete();/删除实验题实验题 2 2 学生信息管理系统。学号:姓名:年龄:查询追加删除状态:图 3.10 学生信息管理系统界面 点击“追加”按钮,可以追加一个学生信息; 点击“查询”按钮和“删除”按钮,可以按照学号查询

11、或者删除一个学生信息;技术提示 可以使用上一题开发的函数。运行结果:添加记录成功后,再次查询,结果如下:然后按学号删除:检验是否删除成功: 可知是删除成功的。代码参考: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.eclip

12、se.swt.widgets.Shell;import org.eclipse.swt.widgets.Label;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;clas

13、s QueryScanner reader=new Scanner(System.in);Connection con;java.sql.PreparedStatement sql;public Query()tryClass.forName(“com.mysql.jdbc.Driver“);catch(ClassNotFoundException e)System.out.println(e);public void select(Text text1,Text text2,Text text3,Text text4)text2.setText(“);text3.setText(“);tex

14、t4.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(“sel

15、ect * 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);public void insert(Text text1,Text text2,T

16、ext 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=trueString user=“root“;String password=“;ResultSet RS;try con=DriverManager.getConnection(URL, user, password); catch (SQLException e) System.out.println(e);try s

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 教育/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号