1 package per.sww.page; 2 3 import per.sww.tooles.ScannerChoice; 4 5 public class MainPage extends ScannerChoice{ 6 public static void main(String[] args) { 7 MainPage.mainpage(); 8 } 9 10 /** 11 * 主页 12 */ 13 public static void mainpage(){ 14 System.out.println("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■"); 15 System.out.println("1.商品维护"); 16 System.out.println("2.前台收银"); 17 System.out.println("3.商品管理"); 18 System.out.println("0.退出系统"); 19 System.out.println("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■"); 20 do { 21 String choice=ScannerInfoString(); 22 String regex="[0-3]"; 23 if(choice.matches(regex)){ 24 int info=Integer.parseInt(choice); 25 switch (info) { 26 case 0: 27 System.out.println("退出完毕!"); 28 System.exit(1); 29 break; 30 case 1: 31 MaintenancePage(); 32 break; 33 case 2: 34 checkstandLogPage(); 35 break; 36 case 3: 37 commodityManagementPage(); 38 break; 39 default: 40 break; 41 } 42 } 43 } while (true); 44 } 45 46 private static void commodityManagementPage() { 47 48 } 49 50 private static void checkstandLogPage() { 51 52 } 53 54 /** 55 * 添加商品页 56 */ 57 public static void MaintenancePage() { 58 System.out.println("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■"); 59 System.out.println("1.添加商品"); 60 System.out.println("2.更改商品"); 61 System.out.println("3.删除商品"); 62 System.out.println("4.查询商品"); 63 System.out.println("5.显示所有商品"); 64 System.out.println("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■"); 65 System.out.println("请输入选项,或者按 0 返回上一级菜单."); 66 do { 67 String choice=ScannerInfoString(); 68 String regex="[0-5]"; 69 if(choice.matches(regex)){ 70 int info=Integer.parseInt(choice); 71 switch (info) { 72 case 0: 73 mainpage(); 74 break; 75 case 1: 76 GoodsPage.addGoodsPage(); 77 break; 78 case 2: 79 break; 80 case 3: 81 break; 82 case 4: 83 break; 84 case 5: 85 break; 86 default: 87 break; 88 } 89 } 90 } while (true); 91 } 92 }
1 package per.sww.tooles; 2 3 import java.util.Scanner; 4 5 import per.sww.page.GoodsPage; 6 import per.sww.page.MainPage; 7 8 public class ScannerChoice { 9 10 /** 11 * 键入提取 12 * @return 13 */ 14 public static String ScannerInfoString() { 15 Scanner sc = new Scanner(System.in); 16 System.out.println("请输入:"); 17 return sc.next(); 18 } 19 /** 20 * 精确到小数点后两位 21 * @return 22 */ 23 public static double ScannerInfo() { 24 double num = 0.00; 25 do { 26 Scanner sc = new Scanner(System.in); 27 String info = sc.next(); 28 String regex = "(([1-9][0-9]*)\\.([0-9]{2}))|[0]\\.([0-9]{2})"; 29 if (info.matches(regex)) { 30 num = Double.parseDouble(info); 31 } else { 32 System.out.println("一分钱都不能少"); 33 continue; 34 } 35 break; 36 } while (true); 37 return num; 38 } 39 40 /** 41 * 商品数量获取 42 * @return 43 */ 44 public static int ScannerNum() { 45 int num = 0; 46 String regex = "([1-9])|([1-9][0-9]+)"; 47 do { 48 Scanner sc = new Scanner(System.in); 49 String info = sc.next(); 50 if (info.matches(regex)) { 51 num = Integer.parseInt(info); 52 } else { 53 System.out.println("你输错了"); 54 continue; 55 } 56 break; 57 } while (true); 58 return num; 59 } 60 61 /** 62 * 获取下一步操作 63 * @param oper 64 */ 65 public static void changedInfoNext(String oper) { 66 do { 67 System.out.println("是否继续进行-当前操作:(Y/N)"); 68 String choice = ScannerChoice.ScannerInfoString(); 69 if ("y".equals(choice) || "Y".equals(choice)) { 70 if ("upateGoodsPage".equals(oper)) { 71 GoodsPage.upateGoodsPage(); 72 } else if ("deleteGoodsPage".equals(oper)) { 73 GoodsPage.deleteGoodsPage(); 74 } else if ("addGoodsPage".equals(oper)) { 75 GoodsPage.addGoodsPage(); 76 } 77 } else if ("n".equals(choice) || "N".equals(choice)) { 78 MainPage.MaintenancePage(); 79 } 80 System.out.println("你输错了"); 81 } while (true); 82 } 83 }
1 package per.sww.entity; 2 3 public class Goods { 4 private int gid; 5 private String gname; 6 private double gprice; 7 private int gnum; 8 9 /** 10 * 商品信息 11 * @param gid 12 * @param gname 13 */ 14 public Goods(int gid,String gname) { 15 this.gid=gid; 16 this.gname=gname; 17 } 18 public Goods(int gid,double gprice){ 19 this.gid=gid; 20 this.gprice=gprice; 21 } 22 public Goods(int gid,int gnum) { 23 this.gid=gid; 24 this.gnum=gnum; 25 } 26 27 /** 28 * 添加商品 29 * @param gname 30 * @param gprice 31 * @param gnum 32 */ 33 public Goods(String gname,double gprice,int gnum){ 34 this.gname=gname; 35 this.gprice=gprice; 36 this.gnum=gnum; 37 } 38 /** 39 * 查看全部信息 40 * @param gid 41 * @param gname 42 * @param gprice 43 * @param gnum 44 */ 45 public Goods(int gid,String gname,double gprice,int gnum){ 46 this.gid=gid; 47 this.gname=gname; 48 this.gprice=gprice; 49 this.gnum=gnum; 50 } 51 52 public int getGid() { 53 return gid; 54 } 55 public void setGid(int gid) { 56 this.gid = gid; 57 } 58 public String getGname() { 59 return gname; 60 } 61 public void setGname(String gname) { 62 this.gname = gname; 63 } 64 public double getGprice() { 65 return gprice; 66 } 67 public void setGprice(double gprice) { 68 this.gprice = gprice; 69 } 70 public int getGnum() { 71 return gnum; 72 } 73 public void setGnum(int gnum) { 74 this.gnum = gnum; 75 } 76 77 }
1 package per.sww.db; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 8 public class DbClose { 9 public static Connection addClose(PreparedStatement ps,Connection conn){ 10 try { 11 if(ps!=null){ 12 ps.close(); 13 } 14 } catch (SQLException e) { 15 e.printStackTrace(); 16 } 17 18 try{ 19 if(conn!=null){ 20 conn.close(); 21 } 22 }catch(SQLException e){ 23 e.printStackTrace(); 24 } 25 return conn; 26 } 27 28 public static Connection queryClose(PreparedStatement ps,Connection conn,ResultSet rs){ 29 try { 30 if(ps!=null){ 31 ps.close(); 32 } 33 } catch (SQLException e) { 34 e.printStackTrace(); 35 } 36 37 try{ 38 if(conn!=null){ 39 conn.close(); 40 } 41 }catch(SQLException e){ 42 e.printStackTrace(); 43 } 44 45 try { 46 if(rs!=null){ 47 rs.close(); 48 } 49 } catch (SQLException e) { 50 e.printStackTrace(); 51 } 52 return conn; 53 } 54 }
1 package per.sww.db; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.SQLException; 6 7 public class DbConn { 8 public static Connection getconn(){ 9 Connection conn=null; 10 String user="root"; 11 String password="root"; 12 String url="jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8"; 13 try { 14 Class.forName("com.mysql.jdbc.Driver"); 15 conn=DriverManager.getConnection(user, password, url); 16 17 } catch (SQLException e) { 18 e.printStackTrace(); 19 } catch (ClassNotFoundException e) { 20 e.printStackTrace(); 21 } 22 return conn; 23 } 24 }
1 package per.sww.page; 2 3 import per.sww.dao.GoodsDao; 4 import per.sww.entity.Goods; 5 import per.sww.tooles.ScannerChoice; 6 7 public class GoodsPage extends ScannerChoice{ 8 9 /** 10 * 输入添加商品页面 11 */ 12 public static void addGoodsPage(){ 13 System.out.println("請輸入商品名称"); 14 String goodsName=ScannerInfoString(); 15 System.out.println("請輸入商品价格"); 16 double goodsPrice=ScannerInfo(); 17 System.out.println("請輸入商品数量"); 18 int goodsNum=ScannerNum(); 19 20 Goods goods=new Goods(goodsName, goodsPrice, goodsNum); 21 boolean bool=new GoodsDao().addGoods(goods); 22 23 if(bool){ 24 System.out.println("放到数据库了"); 25 }else { 26 System.out.println("被人偷了"); 27 } 28 changedInfoNext("addGoodsPage"); 29 } 30 31 public static void upateGoodsPage() { 32 33 } 34 35 public static void deleteGoodsPage() { 36 37 } 38 }
错误提示
java.sql.SQLException: No suitable driver found for root
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at per.sww.db.DbConn.getconn(DbConn.java:15)
at per.sww.dao.GoodsDao.addGoods(GoodsDao.java:24)
at per.sww.page.GoodsPage.addGoodsPage(GoodsPage.java:21)
at per.sww.page.MainPage.MaintenancePage(MainPage.java:76)
at per.sww.page.MainPage.mainpage(MainPage.java:31)
at per.sww.page.MainPage.main(MainPage.java:7)
java.sql.SQLException: No suitable driver found for localhost test
当你JDBC URL 不是能够被一些加载JDBC 驱动所接受,比如 这个Driver#acceptsURL() 方法,
你将会获取到这个错误。你确实忘记了这个JDBC驱动特别的URI 前缀。对于 MYSQL JDBC 驱动 是:jdbc:mysql://. 完整的链接地址应该像:
con = DriverManager.getConnection("jdbc:mysql://localhost/test", user, pass);
同样看:
翻译的不保准,你可以试试。
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
1.商品维护
2.前台收银
3.商品管理
0.退出系统
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
请输入:
1
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
1.添加商品
2.更改商品
3.删除商品
4.查询商品
5.显示所有商品
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
请输入选项,或者按 0 返回上一级菜单.
请输入:
1
請輸入商品名称
请输入:
榴莲
請輸入商品价格
2.11
請輸入商品数量
5
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.goods' doesn't exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2838)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2334)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2262)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2246)
at per.sww.dao.GoodsDao.addGoods(GoodsDao.java:31)
at per.sww.page.GoodsPage.addGoodsPage(GoodsPage.java:21)
at per.sww.page.MainPage.MaintenancePage(MainPage.java:76)
at per.sww.page.MainPage.mainpage(MainPage.java:31)
at per.sww.page.MainPage.main(MainPage.java:7)
被人偷了
是否继续进行-当前操作:(Y/N)
请输入:
宝 宝 心 里 苦
@眉间剪水泛千愁: 报的是数据库 test不存在goods表,你看看数据库test中goods表是否存在
@眉间剪水泛千愁: 上面的connection url是示范的链接格式
@wakasann: (*@ο@*) 哇~,解决了!真厉害
什么错,给出来呀