JDBC 24homework
编写程序:
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的更多相关文章
- Java数据库连接技术——JDBC
大家好,今天我们学习了Java如何连接数据库.之前学过.net语言的数据库操作,感觉就是一通百通,大同小异. JDBC是Java数据库连接技术的简称,提供连接各种常用数据库的能力. JDBC API ...
- 玩转spring boot——结合AngularJs和JDBC
参考官方例子:http://spring.io/guides/gs/relational-data-access/ 一.项目准备 在建立mysql数据库后新建表“t_order” ; -- ----- ...
- [原创]java使用JDBC向MySQL数据库批次插入10W条数据测试效率
使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(100000),如何提高效率呢?在JDBC编程接口中Statement 有两个方法特别值得注意:通过使用addBatch( ...
- JDBC MySQL 多表关联查询查询
public static void main(String[] args) throws Exception{ Class.forName("com.mysql.jdbc.Driver&q ...
- JDBC增加删除修改
一.配置程序--让我们程序能找到数据库的驱动jar包 1.把.jar文件复制到项目中去,整合的时候方便. 2.在eclipse项目右击"构建路径"--"配置构建路径&qu ...
- JDBC简介
jdbc连接数据库的四个对象 DriverManager 驱动类 DriverManager.registerDriver(new com.mysql.jdbc.Driver());不建议使用 ...
- JDBC Tutorials: Commit or Rollback transaction in finally block
http://skeletoncoder.blogspot.com/2006/10/jdbc-tutorials-commit-or-rollback.html JDBC Tutorials: Com ...
- FineReport如何用JDBC连接阿里云ADS数据库
在使用FineReport连接阿里云的ADS(AnalyticDB)数据库,很多时候在测试连接时就失败了.此时,该如何连接ADS数据库呢? 我们只需要手动将连接ads数据库需要使用到的jar放置到%F ...
- JDBC基础
今天看了看JDBC(Java DataBase Connectivity)总结一下 关于JDBC 加载JDBC驱动 建立数据库连接 创建一个Statement或者PreparedStatement 获 ...
随机推荐
- phpstorm clone 码云项目到本地 Version Control 不显示
最近在用码云作为代码仓库,但是建了仓库,也填加了 SSH,把项目利用 phpstorm VCS --> checkout from version control --> git 克隆到 ...
- margin padding border
Difference between margin and padding? Remember these 3 points: The Margin is the extra space around ...
- P2602 [ZJOI2010]数字计数&P1239 计数器&P4999 烦人的数学作业
P2602 [ZJOI2010]数字计数 题解 DFS 恶心的数位DP 对于这道题,我们可以一个数字一个数字的求 也就是分别统计区间 [ L , R ] 内部数字 i 出现的次数 (0<=i&l ...
- goland 可用注册码(license)
N757JE0KCT-eyJsaWNlbnNlSWQiOiJONzU3SkUwS0NUIiwibGljZW5zZWVOYW1lIjoid3UgYW5qdW4iLCJhc3NpZ25lZU5hbWUiO ...
- linux下如何单独编译设备树?
答: make <vendor>/<device_name>.dtb 如: make freescale/fsl-1043a-rdb.dtb
- Kbengine游戏引擎-【4】demo-kbengine_unity3d_demo 在容器docker上安装测试
git地址:https://github.com/kbengine/kbengine_unity3d_demo Demo中文地址:https://github.com/kbengine/kbengin ...
- 表的操作管理和 MySQL 的约束控制
一.表的操作 1.表的基本概念 数据库与表之间的关系:数据库是由各种数据表组成的,数据表是数据库中最重要的对象,用来存储和操作数据的逻辑结构. 表由列和行组成,列是表数据的描述,行是表数据的实例. 表 ...
- List去重为什么要写equals(),hashCode()方法
一,各个集合的特点: Collection(集合):容器,用于存放对象(引用类型.基本类型需要自动装箱) List(列表):元素有序,元素可以重复 (有索引). 通过元素的equals()方法判断是否 ...
- [Feature] Compare the effect of different scalers
Ref: Compare the effect of different scalers on data with outliers 主要是对该代码的学习研究. from sklearn.prepro ...
- Angular05 angular架构、搭建angular开发环境、组件必备三要素、angular启动过程
1 angular架构 1.1 组件:是angular应用的基本构建模块,可以理解为一段带有业务逻辑和数据的HTML 1.2 服务:用来封装可重用的业务逻辑 1.3 指令:允许你想HTML元素添加自定 ...