JDBC(宠物管理系统)
使用纯Java方式连接数据库
1 public Connection getConnection() 2 { 3 try { 4 //1.加载驱动 5 Class.forName("com.mysql.jdbc.Driver"); 6 //forName是静态方法 com.mysql.jdbc是包名 Driver是类名。通过forName方法调用包名为com.mysql.jdbc的包中的Driver方法 7 System.out.println("驱动加载成功!"); 9 String url="jdbc:mysql://localhost:3306/epet?useUnicode=true&characterEncoding=utf-8"; 10 //jdbc:mysql: 是协议 类似于https:// 11 //localhost 是地址 12 //3306 是端口 13 //epet是数据库名 14 String user="root"; 15 String password="123456"; 16 try { 17 con=DriverManager.getConnection(url, user, password); 18 System.out.println("连接成功!"); 19 } catch (SQLException e) { 20 // TODO Auto-generated catch block 21 e.printStackTrace(); 22 } 23 } catch (ClassNotFoundException e) { 24 // TODO Auto-generated catch block 25 e.printStackTrace(); 26 } 27 return con; 28 29 } 30
宠物管理系统菜单
1 public void menu() 2 { 3 do { 4 System.out.println("\t==========宠物管理系统=========="); 5 System.out.println("\t1.添加宠物 \n\t2.更新健康值和亲密度 \n\t3.查看宠物信息\n\t4.删除宠物信息\n\t5.退出"); 6 System.out.print("请选择:"); 7 int choose=input.nextInt(); 8 switch (choose) { 9 case 1: 10 System.out.println("\t========添加宠物========"); 11 add(); 12 continue; 13 case 2: 14 System.out.println("2"); 15 16 continue; 17 case 3: 18 show(); 19 continue; 20 case 4: 21 delete(); 22 continue; 23 case 5: 24 System.out.println("拜拜!"); 25 break; 26 default: 27 System.out.println("选择错误!"); 28 continue; 29 } 30 break; 31 } while (true); 32 }
添加宠物方法
1 public void add() 2 { 3 getConnection(); 4 try { 5 ps=con.prepareStatement("insert into dog(name,strain,health,love)values(?,?,?,?)"); 6 System.out.print("请输入狗狗昵称:"); 7 String name = input.next(); 8 System.out.print("请输入品种:"); 9 String brand=input.next(); 10 System.out.print("请输入健康值:"); 11 int hy=input.nextInt(); 12 System.out.print("请输入亲密值:"); 13 int love = input.nextInt(); 14 ps.setString(1, name); 15 ps.setString(2, brand); 16 ps.setInt(3, hy); 17 ps.setInt(4, love); 18 int count=ps.executeUpdate(); 19 if (count>0) { 20 System.out.println("添加成功!"); 21 show(); 22 }else 23 { 24 System.out.println("添加失败!"); 25 } 26 27 } catch (SQLException e) { 28 // TODO Auto-generated catch block 29 e.printStackTrace(); 30 }finally 31 { 32 try { 33 ps.close(); 34 } catch (SQLException e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 } 38 } 39 }
展示所有宠物信息方法
1 public void show() 2 { 3 getConnection(); 4 try { 5 System.out.println("******************狗狗信息表******************"); 6 System.out.println("编号\t姓名\t健康值\t亲密度\t品种"); 7 ps=con.prepareStatement("select * from dog"); 8 rs=ps.executeQuery(); 9 while (rs.next()) { 10 System.out.print(rs.getInt(1)+"\t"); 11 System.out.print(rs.getString(2)+"\t"); 12 System.out.print(rs.getInt(3)+"\t"); 13 System.out.print(rs.getInt(4)+"\t"); 14 System.out.println(rs.getString(5)+"\t"); 15 } 16 } catch (SQLException e) { 17 // TODO Auto-generated catch block 18 e.printStackTrace(); 19 } 20 finally 21 { 22 try { 23 con.close(); 24 rs.close(); 25 ps.close(); 26 } catch (SQLException e) { 27 // TODO Auto-generated catch block 28 e.printStackTrace(); 29 } 30 31 } 32 }
删除宠物方法
1 public void delete() 2 { 3 try{ 4 ps=con.prepareStatement("delete from dog where id=?"); 5 System.out.println("请输入你要删除的狗狗id"); 6 int id =input.nextInt(); 7 ps.setInt(1, id); 8 ps.setInt(1, 9); 9 int count=ps.executeUpdate(); 10 if (count>0) { 11 System.out.println("删除成功!"); 12 show(); 13 }else 14 { 15 System.out.println("删除失败!"); 16 } 17 } catch (SQLException e) { 18 // TODO Auto-generated catch block 19 e.printStackTrace(); 20 } 21 22 }
调用方法
1 public static void main(String[] args) { 2 // TODO Auto-generated method stub 3 Conn conn= new Conn(); 4 conn.menu(); 5 }