JDBC 24homework

编写程序:

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

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

程序效果如下:

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

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

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

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

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

  1 package com.xt.java.base24;
  2
  3 import java.sql.Connection;
  4 import java.sql.DriverManager;
  5 import java.sql.ResultSet;
  6 import java.sql.SQLException;
  7 import java.sql.Statement;
  8 import java.util.Scanner;
  9
 10 public class UserTest {
 11
 12     public static void main(String[] args) {
 13         while(true){
 14         Scanner scanner=new Scanner(System.in);
 15         System.out.println("----------------欢迎进入商品管理系统-------------------");
 16         System.out.println("插入----------1");
 17         System.out.println("删除----------2");
 18         System.out.println("修改----------3");
 19         System.out.println("查询----------4");
 20         System.out.println("退出----------0\n\n");
 21         System.out.println("请选择您要进行的操作:");
 22         int selectNum=scanner.nextInt();
 23         Connection conn=null;
 24         Statement stat=null;
 25         ResultSet rs=null;
 26         String url="jdbc:mysql://localhost:3306/lyxdatabases";
 27
 28         //加载驱动,通过驱动管理器将Java与数据库建立联系。同时排除异常。
 29         try {
 30             Class.forName("com.mysql.jdbc.Driver");
 31             conn=DriverManager.getConnection(url,"root","1234");
 32             stat=conn.createStatement();
 33         } catch (ClassNotFoundException e) {
 34             e.printStackTrace();
 35         } catch (SQLException e) {
 36             e.printStackTrace();
 37         }
 38
 39
 40         switch(selectNum){
 41         case 0:{
 42             System.out.println("退出系统!!!");
 43             System .exit(0);
 44             break;
 45         }
 46         case 1:{
 47             System.out.println("请输入您要插入的商品的编号:");
 48             int gCode=scanner.nextInt();
 49             System.out.println("请输入您要插入的商品的名称:");
 50             String gName=scanner.next();
 51             System.out.println("请输入您要插入的商品的数量:");
 52             int gNumber=scanner.nextInt();
 53             System.out.println("请输入您要插入的商品的价钱:");
 54             double gPrice=scanner.nextDouble();
 55             String sql="insert into goodsTwo values("+gCode+",‘"+gName+"‘,"+gNumber+","+gPrice+")";
 56             //查看sql语句是否输入正确!
 57             System.out.println(sql);
 58             try {
 59                 int affectedRows=stat.executeUpdate(sql);
 60                 if(affectedRows>0){
 61                     System.out.println("商品插入成功!");
 62                 }else{
 63                     System.out.println("商品插入失败,请重新操作。。。。。。");
 64                 }
 65             } catch (SQLException e) {
 66                 // TODO Auto-generated catch block
 67                 e.printStackTrace();
 68             }
 69             try {
 70                 if(rs!=null){
 71                 rs.close();
 72                 }
 73                 if(stat!=null){
 74                     stat.close();
 75                 }
 76                 if(conn!=null){
 77                     conn.close();
 78                 }
 79             }
 80              catch (SQLException e) {
 81                 // TODO Auto-generated catch block
 82                 e.printStackTrace();
 83
 84             }
 85             break;
 86         }
 87         case 2:{
 88             System.out.println("请输入您要删除的商品的编号:");
 89             int gCode1=scanner.nextInt();
 90             String sql1="delete from goodsTwo where code="+gCode1;
 91             try {
 92                 int affectedRows1=stat.executeUpdate(sql1);
 93                 if(affectedRows1>0){
 94                     System.out.println("商品删除成功!!");
 95                 }else{
 96                     System.out.println("商品删除失败,请重新操作。。。。。。");
 97                 }
 98             } catch (SQLException e) {
 99                 // TODO Auto-generated catch block
100                 e.printStackTrace();
101             }
102             try {
103                 if(rs!=null){
104                 rs.close();
105                 }
106                 if(stat!=null){
107                     stat.close();
108                 }
109                 if(conn!=null){
110                     conn.close();
111                 }
112             }
113              catch (SQLException e) {
114                 // TODO Auto-generated catch block
115                 e.printStackTrace();
116
117             }
118             break;
119         }
120         case 3:{
121             while (true){
122             System.out.println("请输入您要改正的商品的编号:");
123             int gCode2=scanner.nextInt();
124             System.out.println("编号----------a");
125             System.out.println("名称----------b");
126             System.out.println("数量----------c");
127             System.out.println("价钱----------d");
128             System.out.println("操作完成,退出系统----------e");
129
130             System.out.println("----------请选择您要改正的对象:");
131             String mark=scanner.next();
132             /**
133              * 改正编号。。。
134              */
135             if(mark.equals("a")){
136                 System.out.println("请输入你要改正的编号:");
137                 int code2=scanner.nextInt();
138                 String sql2="update goodsTwo set code="+code2+" where code="+gCode2;
139                 //查看sql2语句是否输入正确!
140                 System.out.println(sql2);
141
142                 try {
143                     int affectedRows1=stat.executeUpdate(sql2);
144                     if(affectedRows1>0){
145                         System.out.println("商品改正成功!!");
146                     }else{
147                         System.out.println("商品改正失败,请重新操作。。。。。。");
148                     }
149                 } catch (SQLException e) {
150                     // TODO Auto-generated catch block
151                     e.printStackTrace();
152                 }
153
154             }
155
156             /**
157              * 改正名称。。。。
158              */
159             else if(mark.equals("b")){
160                 System.out.println("请输入你要改正的名称:");
161                 String gName=scanner.next();
162                 String sql3="update goodsTwo set name=‘"+gName+"‘ where code="+gCode2;
163                 //查看sql3语句是否输入正确!
164                 System.out.println(sql3);
165
166                 try {
167                     int affectedRows1=stat.executeUpdate(sql3);
168                     if(affectedRows1>0){
169                         System.out.println("商品改正成功!!");
170                     }else{
171                         System.out.println("商品改正失败,请重新操作。。。。。。");
172                     }
173                 } catch (SQLException e) {
174                     // TODO Auto-generated catch block
175                     e.printStackTrace();
176                 }
177
178             }
179             /**
180              * 改正数量。。。。
181              */
182             else if(mark.equals("c")){
183                 System.out.println("请输入你要改正的数量:");
184                 int gNumber=scanner.nextInt();
185                 String sql4="update goodsTwo set number="+gNumber+" where code="+gCode2;
186                 //查看sql4语句是否输入正确!
187                 System.out.println(sql4);
188
189                 try {
190                     int affectedRows1=stat.executeUpdate(sql4);
191                     if(affectedRows1>0){
192                         System.out.println("商品改正成功!!");
193                     }else{
194                         System.out.println("商品改正失败,请重新操作。。。。。。");
195                     }
196                 } catch (SQLException e) {
197                     // TODO Auto-generated catch block
198                     e.printStackTrace();
199                 }
200                 try {
201                     if(rs!=null){
202                     rs.close();
203                     }
204                     if(stat!=null){
205                         stat.close();
206                     }
207                     if(conn!=null){
208                         conn.close();
209                     }
210                 }
211                  catch (SQLException e) {
212                     // TODO Auto-generated catch block
213                     e.printStackTrace();
214
215                 }
216
217             }
218             /**
219              * 改正价钱。。。
220              */
221             else if(mark.equals("d")){
222                 System.out.println("请输入你要改正的价钱:");
223                 double gPrice2=scanner.nextDouble();
224                 String sql4="update goodsTwo set price="+gPrice2+" where code="+gCode2;
225                 //查看sql4语句是否输入正确!
226                 System.out.println(sql4);
227
228                 try {
229                     int affectedRows1=stat.executeUpdate(sql4);
230                     if(affectedRows1>0){
231                         System.out.println("商品改正成功!!");
232                     }else{
233                         System.out.println("商品改正失败,请重新操作。。。。。。");
234                     }
235                 } catch (SQLException e) {
236                     // TODO Auto-generated catch block
237                     e.printStackTrace();
238                 }
239
240             }
241             else if(mark.equals("e")){
242                 System.out.println("操作完成,退出系统!!");
243                 return;
244             }
245             else{
246                 System.out.println("您输入的数据有错误,请重新输入!!!");
247                 break;
248             }
249
250
251         }
252
253         }
254         case 4:{
255             System.out.println("请输入您要查询的商品的编号:");
256             int gCode5=scanner.nextInt();
257             String sql5="select* from goodsTwo where code="+gCode5;
258             try {
259                 rs=stat.executeQuery(sql5);
260                 while(rs.next()){
261                 System.out.print("编号:"+rs.getInt("code")+"    ");
262                 System.out.print("名称:"+rs.getString("name")+"   ");
263                 System.out.print("数量:"+rs.getInt("number")+"   ");
264                 System.out.println("价钱:"+rs.getDouble("price"));}
265             } catch (SQLException e) {
266                 // TODO Auto-generated catch block
267                 e.printStackTrace();
268             }
269
270
271         }
272
273         try {
274             if(rs!=null){
275             rs.close();
276             }
277             if(stat!=null){
278                 stat.close();
279             }
280             if(conn!=null){
281                 conn.close();
282             }
283         }
284          catch (SQLException e) {
285             // TODO Auto-generated catch block
286             e.printStackTrace();
287
288         }
289         break;
290         }
291
292
293         }
294
295     }
296
297 }
时间: 2024-08-13 11:01:15

JDBC 24homework的相关文章

jdbc的简单使用

1.加载驱动(mysql的驱动是com.mysql.jdbc.Driver,SqlServer的驱动是 com.microsoft.sqlserver.jdbc.SQLServerDriver) 2.加载数据库的连接(url, username,password) 3.编写sql语句(String sql="select * from grade  where gradeName = ?";) 4.遍历查询结果 [while (resultSet.next()) {   System.

商城项目整理(三)JDBC增删改查

商品表的增加,修改,删除,订单表的增加,确认,用户表的查看,日志表的增加,查看 商品表建表语句: 1 create table TEST.GOODS_TABLE 2 ( 3 gid NUMBER not null, 4 gname VARCHAR2(90), 5 gdetails CLOB, 6 gpicture VARCHAR2(100), 7 gprice NUMBER, 8 gleixing NUMBER, 9 gpinpai VARCHAR2(20) 10 ) 11 tablespace

jdbc驱动jar导入eclipse

在使用JDBC编程时需要连接数据库,导入JAR包是必须的,导入其它的jar包方法同样如此,导入的方法是 打开eclipse 1.右击要导入jar包的项目,点properties 2.左边选择java build path,右边选择libraries 3.选择add External jars 4.选择jar包的按照路径下的确定后就行了. Java连接MySQL的最新驱动包下载地址 http://www.mysql.com/downloads/connector/j 有两种方法导入jar包,第一种

JDBC

Java语言访问数据库的一种规范,是一套API JDBC (Java Database Connectivity) API,即Java数据库编程接口,是一组标准的Java语言中的接口和类,使用这些接口和类,Java客户端程序可以访问各种不同类型的数据库.比如建立数据库连接.执行SQL语句进行数据的存取操作. JDBC规范采用接口和实现分离的思想设计了Java数据库编程的框架.接口包含在java.sql及javax.sql包中,其中java.sql属于JavaSE,javax.sql属于JavaE

java链接MySQL数据库时使用com.mysql.jdbc.Connection的包会出红线问题

package com.swift; //这里导入的包是java.sql.Connection而不是com.mysql.jdbc.Connection import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class TestJDBC { public static void main(String[] args) { try { Class.forName(

Tomcat JDBC Pool使用说明

Maven依赖 <dependency>   <groupId>org.apache.tomcat</groupId>   <artifactId>tomcat-jdbc</artifactId>   <version>8.5.9</version> </dependency> 最新版本为9.0,推荐使用8.5.9稳定版 常用配置 连接池配置项很多,下面只列出了推荐配置,项目组可根据自身情况进行增减 <b

tomcat启动过程报the JDBC Driver has been forcibly unregistered问题的修复过程

最近两天在整理关于flume的总结文档,没有启动过tomcat.昨天晚上部署启动,发现报了如题的错误,全文如下: 严重: The web application [/oa-deploy] registered the JBDC driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] but failed to unregister it when the web application was stopped. To prevent a mem

使用JDBC如何提高访问数据库的性能?

1. 使用数据连接池(Connection Pool), 避免使用DriverManager.getConnection. 2. 合理的配置数据连接池参数,设置数据连接池的初始大小,最大连接数,连接超时时间等. 3. 选择合适的事务等级,按照不同的数据库操作类型选择不同的事务等级. 4. 及时关闭Connection,不关闭的话会严重影响系统的性能,甚至造成系统罢工. 5. 优化Statement 1) 选择合适的Statement, 根据不同的数据库操作选择Statement, Prepare

回滚的意义---JDBC事务回滚探究

JDBC手动事务提交回滚的常见写法一直是rollback写在commit的catch之后: try{ conn.setAutoCommit(false); ps.executeUpdate(); ps.executeUpdate(); conn.commit(); }catch(Exception e){ conn.rollback(); } 但是,这种回滚是没有意义的: 一旦commit前出错, 就不提交了, 回滚无用 一旦commit了, 说明没错, 不用回滚 找到一篇和我观点相同的文章: