实验3---JDBC编程1

2020-02-24  本文已影响0人  Riya

一、实验目的

本实验的目的是掌握数据库环境的搭建;掌握使用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();
}
}
}
上一篇下一篇

猜你喜欢

热点阅读