java数据库完成增删改查

一,问题由来

初学者最开始做管理系统时,是在控制台完成,给定客户端,然后输入具体操作完成,而问题就出来了,只要关闭eclipse数据就会消失,这样,会带来很大的不便,然后,我们开始尝试用文本的方式保存信息,但是这样的操作又太过于繁琐,最后,我想给定最好的解决办法,利用数据库来完成存储的操作

二,实例解析

创建表

在这里插入图片描述

编写java代码

package com.lianxi;

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 Demo {
	//给定boolean值做判断
	private boolean isTrue=true;
	//创建start方法
	public void start() throws ClassNotFoundException{
		//给定扫描器,用来输入操作指令
		Scanner sc=new Scanner(System.in);
		//给定具体操作有哪些
		System.out.println("请输入操作1(添加)2(查询)3(删除)4(修改)5(退出)");
		//根据用户输入的值来做相应的操作
		int key=sc.nextInt();
		//给定死循环,直到用户输入退出才退出程序
		while(isTrue){
			switch(key){
			case 1:add();break;
			case 2:find();break;
			case 3:delete();break;
			case 4:update();break;
			case 5:exit();break;
			default:
				System.out.println("没有该操作,请重新输入操作指令!");
				start();
				break;
			}
		}
	}
	private void update() throws ClassNotFoundException{
		Class.forName("com.mysql.jdbc.Driver");
		String url="jdbc:mysql://localhost:3306/testaa";
		String user="root";
		String password=("123456");
		Connection cn=null;
		Statement sta=null;
		 try {
			cn = DriverManager.getConnection(url,user,password);
			String s="update department set depname='学习部' where depno in (2,3)";
			 sta = cn.createStatement();
			 sta.executeUpdate(s);
			 System.out.println("修改成功!");
			 start();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				cn.close();
				sta.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	private void delete() throws ClassNotFoundException {
		Class.forName("com.mysql.jdbc.Driver");
		String url="jdbc:mysql://localhost:3306/testaa";
		String user="root";
		String password="123456";
		Connection cn=null;
		Statement sta=null;
		      try {
				cn = DriverManager.getConnection(url,user,password);
				  String s="delete from department where depno=5";
				   sta = cn.createStatement();
				       sta.executeUpdate(s);
				       System.out.println("删除成功");
				       start();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				try {
					sta.close();
					cn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		
	}
	private void find() throws ClassNotFoundException {
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		String url="jdbc:mysql://localhost:3306/testaa";
		String user="root";
		String password="123456";
		Connection cn=null;
		Statement sta =null;
		ResultSet res=null;
		try {
			cn = DriverManager.getConnection(url,user,password);
			 String s="Select * from department";
			 sta = cn.createStatement();
			 res= sta.executeQuery(s);
			 //判断表里是否有值,有输出,无则停止
			 while(res.next()){
				 int did=res.getInt(1);
				 String dname=res.getString(2);
				 String dmark=res.getString(3);
				 System.out.println(did+" "+dname+" "+dmark);
			 }
			 //回调方法
			 start();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				res.close();
				sta.close();
				cn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	//添加数据
	private void add() throws ClassNotFoundException {
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		//指定库路径
		String url="jdbc:mysql://localhost:3306/testaa";
		//确定用户名
		String user="root";
		//确定密码
		String password="123456";
		//初始化
		Connection cn=null;
		Statement sta=null;
		   try {
			   //驱动器获取连接
			cn = DriverManager.getConnection(url,user,password);
			//给定添加SQL语句
			String s="insert into department(depname,remark) values('财务部','300')";
			//创建连接对象
			sta = cn.createStatement();
			//运行SQL语句的操作
			sta.executeUpdate(s);
			//返回一个友好提示
			System.out.println("添加成功!");
			//回调方法
			start();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				//关闭连接
				cn.close();
				//关闭连接对象
				sta.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	//退出系统
	private void exit() {
		System.out.println("谢谢使用,下次再见!");
		System.exit(1);
	}

	public static void main(String[] args) throws ClassNotFoundException {
		//创建对象,调用start方法
		Demo d=new Demo();
		d.start();
	}
}

测试结果

在这里插入图片描述
在这里插入图片描述

小结

利用数据库我们可以很好的操作数据,保存数据,但是数据库的连接步骤要记住:1,添加数据库驱动;2,指定库的路径;3,指定登入用户名和密码;4,驱动器获取连接;5,给定想操作的数据库指定;6,驱动器创建连接对象;7,连接对象调用方法执行数据库指令;8,关闭驱动连接和连接对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值