美文网首页
实验3---JDBC编程1

实验3---JDBC编程1

作者: Riya | 来源:发表于2020-02-24 13:30 被阅读0次

一、实验目的

本实验的目的是掌握数据库环境的搭建;掌握使用JDBC访问数据库的步骤;掌握使用Java API操作数据库。

二、实验内容

创建一个Product(Product_Code CHAR(7), Description VARCHAR(40), Price DECIMAL(10, 2))表,建议使用PreparedStatement接口,编程实现以下功能:

  1. 实现对表的创建操作;对表完成增删改查等基本操作。
  2. 由用户输入价格参数,根据用户输入的价格查找所有产品的价格大于用 户输入价格的产品信息。
  3. 由用户输入价格参数,将产品'Toaster'的价格调整为用户输入的价格。

三、实验步骤

  1. 连接数据库。
  2. 通过PreparedStatement接口创建表
  3. 使用PreparedStatement执行动态的插入数据的SQL语句,在动态的SQL语句中使用“?”作为动态参数的占位符。使用executeUpdate():在此PreparedStatement对象中执行SQL语句。
  4. 使用executeQuery():在此PreparedStatement对象中执行查询数据的SQL语句,并返回该查询生成的ResultSet对象。
  5. 使用executeUpdate():在PreparedStatement对象中执行删除数据的SQL语句。

Ps:关于对表的增删改查以及其他的功能操作十分类似,都是根据需要选择 executeUpdate():或executeQuery():在PreparedStatement对象中执行相应的增 删改查的SQL语句。

四、实验结果

五、实验小结

在选择PreparedStatement中的方法时应该注意,executeUpdate():是返回SQL语句所影响的行数;而executeQuery():返回的是一个结果集。只要区分好这两点,然后按照你要实现的功能选择好方法就好了!

六、源代码

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class ConnectionDemo {

public static void main(String[] args) throws Exception {
try {
// 加载microsoft驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 建立数据库连接
String  url="jdbc:mysql://localhost:3306/database";
String user="root";
String password="1009707126zxy+";
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println("连接成功!");
PreparedStatement ps = null;
//建表
String sql1 = "create table  Product(Product_Code CHAR(7),Description VARCHAR(40),Price DECIMAL(10, 2))";
ps = conn.prepareStatement(sql1); 
ps.executeUpdate();

//插入
// 创建Statment对象
Statement stmt = conn.createStatement();
// 获取查询结果集
Scanner in=new Scanner(System.in);
System.out.print("输入商品编号:");
String code=in.nextLine();
System.out.print("输入商品名:");
String goods=in.nextLine();
System.out.print("输入商品价格:");
double price=in.nextDouble();
//关闭载体
stmt.close();
//关闭
String str1="insert into Product(Product_Code,Description,Price) values(?,?,?)";
ps=conn.prepareStatement(str1);
ps.setString(1, code);
ps.setString(2, goods);
ps.setDouble(3, price);
int count=ps.executeUpdate();
if(count==1){
    System.out.println("插入成功!");
}
//查询表内容
System.out.println("第一次查询表内容(删除前;删除商品编码为1的商品信息)");
ps = conn.prepareStatement("select * from Product");
ResultSet result1 = ps.executeQuery();
while(result1.next())
System.out.println(result1.getString("Product_Code")+"\t"+result1.getString("Description")+"\t"+result1.getDouble("Price"));
//删除表中数据
ps = conn.prepareStatement("delete from Product where Product_Code=1");
ps.executeUpdate();

//查询表中内容
System.out.println("第二次查询表内容(删除后)");
ps = conn.prepareStatement("select * from Product");
ResultSet result2 = ps.executeQuery();
while(result2.next())
System.out.println(result2.getString("Product_Code")+"\t"+result2.getString("Description")+"\t"+result2.getDouble("Price"));

//修改
String sql3="Update Product set Price=10 where Product_Code=3";
ps = conn.prepareStatement(sql3);
ps.executeUpdate();
//查询
System.out.println("第三次查询表内容(修改后;将商品名为3的商品价格修改为10)");
ps = conn.prepareStatement("select * from Product");
ResultSet result3 = ps.executeQuery();
while(result3.next())
System.out.println(result3.getString("Product_Code")+"\t"+result3.getString("Description")+"\t"+result3.getDouble("Price"));
//实现功能:用户输入价格参数,根据用户输入的价格查找所有产品的价格大于用户输入价格的产品信息
System.out.println("请输入商品价格(将输出大于这个价格的所有产品信息):");
Scanner input=new Scanner(System.in);
double price1=input.nextDouble();
ps = conn.prepareStatement("select * from Product where price>"+price1+"");
System.out.println("大于这个价格的所有商品信息:");
ResultSet result4 = ps.executeQuery();
while(result4.next())
System.out.println(result4.getString("Product_Code")+"\t"+result4.getString("Description")+"\t"+result4.getDouble("Price"));
//由用户输入价格参数,将产品'Toaster'的价格调整为用户输入的价格
System.out.println("请输入商品价格(将产品'Toaster'的价格调整为用户输入的价格):");
double price2=input.nextDouble();
String sql2="update Product set Price="+price2+" where Description='Toaster'";
ps = conn.prepareStatement(sql2);
ps.executeUpdate();
//查询
System.out.println("第四次查询表内容(修改‘Toaster’商品价格后):");
ps = conn.prepareStatement("select * from Product");
ResultSet result5 = ps.executeQuery();
while(result5.next())
System.out.println(result5.getString("Product_Code")+"\t"+result5.getString("Description")+"\t"+result5.getDouble("Price"));
//删除表
ps = conn.prepareStatement("drop table Product");
ps.executeUpdate(); 
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

相关文章

  • 实验3---JDBC编程1

    一、实验目的 本实验的目的是掌握数据库环境的搭建;掌握使用JDBC访问数据库的步骤;掌握使用Java API操作数...

  • Hadoop实验——MapReduce编程(1)

    实验目的 通过实验掌握基本的MapReduce编程方法。 掌握用MapReduce解决一些常见的数据处理问题,包括...

  • 嵌入式lab3

    嵌入式系统导论实验报告 1.实验题目 DOL的编程与实例分析 2.实验结果 (1)修改example2中的文件,使...

  • 基于C++实现校园卡管理系统

    1 实验目的 本实验面向 C++语言的初学者 主要让实验者熟悉面向对象的编程思想以及类的使用 2 实验环境 本实验...

  • 数字图像处理实验一

    MATLAB入门及数字图像处理编程基础 【实验目的】: 1. 熟悉和掌握MATLAB基本编程环境 2. 熟悉和掌握...

  • 实验1-拓展编程题

    (1)输出短句(What is a computer?);在屏幕上显示一个短句"What is a compu...

  • 实验2-陈淼-0213

    一、实验要求 通过keil编程,在小板实验板中实现按键加1的四位显示效果。 二、实验器材 C52单片机、笔记本电脑...

  • 实验楼小程序上线,手机随时看教程!

    经过1个多月的筹备,实验楼小程序终于上线了。 500门课程+1000多个编程实验,以后打开手机,就可以轻松看文档、...

  • 学习编程的干货网站

    学习编程的干货网站 1.实验楼 https://www.shiyanlou.com/ 2.菜鸟教程 http://...

  • 嵌入式系统导论实验报告 一、实验目的 1、进一步掌握本学期以来所做实验用到的各种元器件的使用方法和编程;2、加深G...

网友评论

      本文标题:实验3---JDBC编程1

      本文链接:https://www.haomeiwen.com/subject/sjxvqhtx.html