JDBC初接触

DBUtil类原生代码:

static {  // 
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	public static final String URL = "jdbc:mysql://localhost:3306/zking1223?characterEncoding=utf-8";
	public static final String USER = "root";
	public static final String PASSWORD = "123456";
	
	// 开连接
	public static Connection getConnection() {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(URL, USER, PASSWORD);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	// 关连接
	public static void close(Connection conn,PreparedStatement ps) {
		try {
			ps.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	// 查询时的关连接操作
	public static void close(Connection conn,PreparedStatement ps,ResultSet rs) {
		try {
			if(rs!=null)rs.close();
			if(ps!=null)ps.close();
			if(conn!=null)conn.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

增删改查操作

public static Scanner sc = new Scanner(System.in); // 创建全局扫描器
	static Connection conn = DBUtil.getConnection();
	static PreparedStatement ps = null;
	static ResultSet rs = null;
	// 查看所有女神信息
	public static void viewAll() {

		try {
			String sql = "select * from goddess";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			System.out.println("****************************************");
			System.out.println("编号/t姓名/t年龄/t手机号");
			while(rs.next()) {
				int id = rs.getInt("id");
				String name = rs.getString("name");
				int age = rs.getInt("age");
				String tel = rs.getString("tel");
				System.out.println(id+"\t"+name+"\t"+age+"\t"+tel);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			
			System.out.println("****************************************");
		}
		
		
	}
	// 查询某一位女神信息
	public static void info() {
		try {
			System.out.println("请输入您要查看女神的姓名:");
			String inputName = sc.next();
			String sql = "select * from goddess where name = ?";
			ps = conn.prepareStatement(sql); // 创建执行对象
			ps.setString(1, inputName); // 传参
			rs = ps.executeQuery(); // 执行查询操作,可以读取信息;
			System.out.println("姓名\t年龄\t手机号");
			if (rs.next()) {
				String name = rs.getString("name");
				int age = rs.getInt("age");
				String tel = rs.getString("tel");
				System.out.println(name+"\t"+age+"\t"+tel);
			}
			else System.out.println("没有这个人");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void info(String name) {

		try {
			
			String sql = "select * from goddess where name = ?";
			ps = conn.prepareStatement(sql); // 创建执行对象
			ps.setString(1, name); // 传参
			rs = ps.executeQuery(); // 执行查询操作,可以读取信息;
			System.out.println("编号\t姓名\t年龄\t手机号");
			if (rs.next()) {
				int id = rs.getInt("id");
				String name1 = rs.getString("name");
				int age = rs.getInt("age");
				String tel = rs.getString("tel");
				System.out.println(id+"\t"+name1+"\t"+age+"\t"+tel);
			}
			else System.err.println("没有这个人");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	// 添加女神信息
	public static void add() {

		try {
			System.out.println("请输入女神的姓名:");
			String name = sc.next(); // 输入姓名
			System.out.println("请输入女神的年龄:");
			int age = sc.nextInt(); // 输入年龄
			System.out.println("请输入女神的电话号码:");
			String tel = sc.next(); // 输入电话号码
		
			String sql = "insert into goddess values(null,?,?,?)"; // 指定sql语句
			ps = conn.prepareStatement(sql); // 创建执行对象
			// 传参
			ps.setString(1, name);
			ps.setInt(2, age);
			ps.setString(3, tel);
			int count = ps.executeUpdate(); // 执行增加操作,返回受影响行数
			if (count>0) {
				System.out.println("女神库里又多了一位女神");
			}
			else System.out.println("未知原因,添加失败");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	// 更新某位女神的所有信息
	public static void upd() {
		try {
			System.out.println("请输入你更改信息的女神姓名:");
			String inputName = sc.next();
			System.out.println("女神当前信息为:");
			info(inputName);
			System.out.println("请输入要修改的姓名:");
			String updName = sc.next();
			System.out.println("请输入要修改的年龄:");
			int updAge = sc.nextInt();
			System.out.println("请输入要修改的电话号码:");
			String updTel = sc.next();
			String sql = "update goddess set name = ?,age = ?, tel = ? where name = ? ";
			ps = conn.prepareStatement(sql);
			ps.setString(1, updName);
			ps.setInt(2, updAge);
			ps.setString(3, updTel);
			ps.setString(4, inputName);
			int count = ps.executeUpdate();
			if (count > 0) {
				System.out.println("更新成功");
				System.out.println("更新之后的信息为:");
				info(updName);
			}
			else System.out.println("更新失败,请重试");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// 删除女神信息(按姓名删除)
	public static void del() {
		try {
			System.out.println("请输入你更改信息的女神姓名:");
			String inputName = sc.next();
			String sql = "delete from goddess where name = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, inputName);
			int count = ps.executeUpdate();
			if (count>0) {
				System.out.println("删除成功");
			}
			else System.out.println("删除失败");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

JDBC初接触

上一篇:SQLSERVER 自增ID插入指定数据 与 获取表的下一个自增ID


下一篇:Elasticsearch数据库优化实战:让你的ES飞起来