编写程序:

1. 创建商品信息表Goods(包含编号Code、名称Name、数量Number、单价Price)

2. 设计程序流程,由用户选择:插入、删除、修改、查询

程序效果如下:

(1)根据提示输入:1.插入 2.删除 3.修改 4.查询

(2)如果用户选择1. 则提示用户输入商品的编号、名称、数量、单价信息,并将用户输入的数据insert到表中。

(3)如果用户选择2,则表示用户要删除一个商品,提示用户输入商品编号,并根据用户输入的编号删除表中的记录。

(4)自行设计修改的实现方法

(5)自行设计查询的方法

 package com.xt.java.base24;

 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner; public class UserTest { public static void main(String[] args) {
while(true){
Scanner scanner=new Scanner(System.in);
System.out.println("----------------欢迎进入商品管理系统-------------------");
System.out.println("插入----------1");
System.out.println("删除----------2");
System.out.println("修改----------3");
System.out.println("查询----------4");
System.out.println("退出----------0\n\n");
System.out.println("请选择您要进行的操作:");
int selectNum=scanner.nextInt();
Connection conn=null;
Statement stat=null;
ResultSet rs=null;
String url="jdbc:mysql://localhost:3306/lyxdatabases"; //加载驱动,通过驱动管理器将Java与数据库建立联系。同时排除异常。
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url,"root","1234");
stat=conn.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} switch(selectNum){
case 0:{
System.out.println("退出系统!!!");
System .exit(0);
break;
}
case 1:{
System.out.println("请输入您要插入的商品的编号:");
int gCode=scanner.nextInt();
System.out.println("请输入您要插入的商品的名称:");
String gName=scanner.next();
System.out.println("请输入您要插入的商品的数量:");
int gNumber=scanner.nextInt();
System.out.println("请输入您要插入的商品的价钱:");
double gPrice=scanner.nextDouble();
String sql="insert into goodsTwo values("+gCode+",'"+gName+"',"+gNumber+","+gPrice+")";
//查看sql语句是否输入正确!
System.out.println(sql);
try {
int affectedRows=stat.executeUpdate(sql);
if(affectedRows>0){
System.out.println("商品插入成功!");
}else{
System.out.println("商品插入失败,请重新操作。。。。。。");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(rs!=null){
rs.close();
}
if(stat!=null){
stat.close();
}
if(conn!=null){
conn.close();
}
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace(); }
break;
}
case 2:{
System.out.println("请输入您要删除的商品的编号:");
int gCode1=scanner.nextInt();
String sql1="delete from goodsTwo where code="+gCode1;
try {
int affectedRows1=stat.executeUpdate(sql1);
if(affectedRows1>0){
System.out.println("商品删除成功!!");
}else{
System.out.println("商品删除失败,请重新操作。。。。。。");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(rs!=null){
rs.close();
}
if(stat!=null){
stat.close();
}
if(conn!=null){
conn.close();
}
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace(); }
break;
}
case 3:{
while (true){
System.out.println("请输入您要改正的商品的编号:");
int gCode2=scanner.nextInt();
System.out.println("编号----------a");
System.out.println("名称----------b");
System.out.println("数量----------c");
System.out.println("价钱----------d");
System.out.println("操作完成,退出系统----------e"); System.out.println("----------请选择您要改正的对象:");
String mark=scanner.next();
/**
* 改正编号。。。
*/
if(mark.equals("a")){
System.out.println("请输入你要改正的编号:");
int code2=scanner.nextInt();
String sql2="update goodsTwo set code="+code2+" where code="+gCode2;
//查看sql2语句是否输入正确!
System.out.println(sql2); try {
int affectedRows1=stat.executeUpdate(sql2);
if(affectedRows1>0){
System.out.println("商品改正成功!!");
}else{
System.out.println("商品改正失败,请重新操作。。。。。。");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } /**
* 改正名称。。。。
*/
else if(mark.equals("b")){
System.out.println("请输入你要改正的名称:");
String gName=scanner.next();
String sql3="update goodsTwo set name='"+gName+"' where code="+gCode2;
//查看sql3语句是否输入正确!
System.out.println(sql3); try {
int affectedRows1=stat.executeUpdate(sql3);
if(affectedRows1>0){
System.out.println("商品改正成功!!");
}else{
System.out.println("商品改正失败,请重新操作。。。。。。");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
/**
* 改正数量。。。。
*/
else if(mark.equals("c")){
System.out.println("请输入你要改正的数量:");
int gNumber=scanner.nextInt();
String sql4="update goodsTwo set number="+gNumber+" where code="+gCode2;
//查看sql4语句是否输入正确!
System.out.println(sql4); try {
int affectedRows1=stat.executeUpdate(sql4);
if(affectedRows1>0){
System.out.println("商品改正成功!!");
}else{
System.out.println("商品改正失败,请重新操作。。。。。。");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(rs!=null){
rs.close();
}
if(stat!=null){
stat.close();
}
if(conn!=null){
conn.close();
}
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace(); } }
/**
* 改正价钱。。。
*/
else if(mark.equals("d")){
System.out.println("请输入你要改正的价钱:");
double gPrice2=scanner.nextDouble();
String sql4="update goodsTwo set price="+gPrice2+" where code="+gCode2;
//查看sql4语句是否输入正确!
System.out.println(sql4); try {
int affectedRows1=stat.executeUpdate(sql4);
if(affectedRows1>0){
System.out.println("商品改正成功!!");
}else{
System.out.println("商品改正失败,请重新操作。。。。。。");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
else if(mark.equals("e")){
System.out.println("操作完成,退出系统!!");
return;
}
else{
System.out.println("您输入的数据有错误,请重新输入!!!");
break;
} } }
case 4:{
System.out.println("请输入您要查询的商品的编号:");
int gCode5=scanner.nextInt();
String sql5="select* from goodsTwo where code="+gCode5;
try {
rs=stat.executeQuery(sql5);
while(rs.next()){
System.out.print("编号:"+rs.getInt("code")+" ");
System.out.print("名称:"+rs.getString("name")+" ");
System.out.print("数量:"+rs.getInt("number")+" ");
System.out.println("价钱:"+rs.getDouble("price"));}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } try {
if(rs!=null){
rs.close();
}
if(stat!=null){
stat.close();
}
if(conn!=null){
conn.close();
}
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace(); }
break;
} } } }

JDBC 24homework的更多相关文章

  1. Java数据库连接技术——JDBC

    大家好,今天我们学习了Java如何连接数据库.之前学过.net语言的数据库操作,感觉就是一通百通,大同小异. JDBC是Java数据库连接技术的简称,提供连接各种常用数据库的能力. JDBC API ...

  2. 玩转spring boot——结合AngularJs和JDBC

    参考官方例子:http://spring.io/guides/gs/relational-data-access/ 一.项目准备 在建立mysql数据库后新建表“t_order” ; -- ----- ...

  3. [原创]java使用JDBC向MySQL数据库批次插入10W条数据测试效率

    使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(100000),如何提高效率呢?在JDBC编程接口中Statement 有两个方法特别值得注意:通过使用addBatch( ...

  4. JDBC MySQL 多表关联查询查询

    public static void main(String[] args) throws Exception{ Class.forName("com.mysql.jdbc.Driver&q ...

  5. JDBC增加删除修改

    一.配置程序--让我们程序能找到数据库的驱动jar包 1.把.jar文件复制到项目中去,整合的时候方便. 2.在eclipse项目右击"构建路径"--"配置构建路径&qu ...

  6. JDBC简介

    jdbc连接数据库的四个对象 DriverManager  驱动类   DriverManager.registerDriver(new com.mysql.jdbc.Driver());不建议使用 ...

  7. JDBC Tutorials: Commit or Rollback transaction in finally block

    http://skeletoncoder.blogspot.com/2006/10/jdbc-tutorials-commit-or-rollback.html JDBC Tutorials: Com ...

  8. FineReport如何用JDBC连接阿里云ADS数据库

    在使用FineReport连接阿里云的ADS(AnalyticDB)数据库,很多时候在测试连接时就失败了.此时,该如何连接ADS数据库呢? 我们只需要手动将连接ads数据库需要使用到的jar放置到%F ...

  9. JDBC基础

    今天看了看JDBC(Java DataBase Connectivity)总结一下 关于JDBC 加载JDBC驱动 建立数据库连接 创建一个Statement或者PreparedStatement 获 ...

随机推荐

  1. 状压dp之不相连块

    传送门 一块田里草地格子不能相邻,问有几种方案. 预处理不相邻块 #include<iostream> #include<cstdio> #include<algorit ...

  2. 处理flutter http请求添加application/json报错Cannot set the body fields of a Request with content-type “application/json”

    在flutter中在http请求发送时设置"content-type": "application/json"会出现报错Cannot set the body ...

  3. Go语言中new和make的区别

    Go语言中new跟make是内置函数,主要用来创建分配类型内存. new( ) new(T)创建一个没有任何数据的类型为T的实例,并返回该实例的指针: 源码解析 func new func new(T ...

  4. pm2 配合log4js处理日志

    1.pm2启动时通常会发现log4js记录不到日志信息: 2.决解方案,安装pm2的pm2-intercom进程间通信模块 3.在log4js的配置文件logger.js里添加如下命令: pm2: t ...

  5. [Java]使用Collections.Max,Min方法取集合类的最大最小值

    代码: package com.hy; import java.util.Arrays; import java.util.Collections; import java.util.List; pu ...

  6. Mac配置jdk以及maven

    一 Mac配置JDK和Maven 1.安装成功jdk后 2.打开终端后,输入vim ~/.bash_profile 3.输入: export MAVEN_HOME=/usr/local/maven/a ...

  7. idea svn 主干分支切换

    使用快捷键ctrl+t或者点击工具栏中的 vcs update 出现如下窗口:点击复选框,在框2中进行选择你要切换的分支的地址,点击OK即可 : 查看是否切换成功可以再version control ...

  8. android Activity,Fragment,Application内存状态监听及等级

    @Override public void onTrimMemory(int level) { super.onTrimMemory(level); switch (level){ case TRIM ...

  9. delphi treeview的子节点图标?

    代码实现不同的子节点图标效果. unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, ...

  10. 【JVM学习笔记】动态代理

    基于JDK的动态代理例子如下 接口 Subject public interface Subject { public abstract void request(); } 实现类RealSubjec ...