MySQL数据库的增删改查JDBC连接实现!

2 篇文章 0 订阅
1 篇文章 0 订阅

数据查询:


/**
	 * 登录/查询
	 * 
	 * @param user
	 * @param password
	 * @throws ClassNotFoundException
	 */
	public static void login(String user, String password) {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			// 1、加载驱动//6.0以下数据库需要加载
			// Class.forName("com.mysql.jdbc.Driver");

			// 2、建立连接
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/user?serverTimezone=GMT&useSSL=false",
					"root", "root");

			// 3、操作数据库
			// 1)预编译SQL格式
			String sql = "SELECT * FROM student WHERE name=? AND password=?";
			statement = connection.prepareStatement(sql);
			// 2)给SQL中的占位符设置值
			statement.setString(1, user); // 给语句中的第一个问号设置值
			statement.setString(2, password); // 给语句中的第二个问号设置值
			// 3)执行查询
			resultSet = statement.executeQuery();
			// 4)从resultset中获取结果
			if (resultSet.next()) {
				System.out.println("登录成功,当前登录用户:" + user + ",性别:" + resultSet.getString("sex"));
			} else {
				System.out.println("登录失败,用户名或密码错误!");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 4、关闭连接
			try {
				if (resultSet != null) {
					resultSet.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (statement != null) {
					statement.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}


数据新增:

Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		// 连接数据库
		try {
			connection = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/server?serverTimezone=GMT&useSSL=false", "root", "root");
			// 新增数据的sql语句
			String sql = "insert into user values(?,?,?,?) ";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, username);
			preparedStatement.setString(2, password);
			preparedStatement.setString(3, name);
			preparedStatement.setInt(4, age);
			preparedStatement.executeUpdate();
		//关闭连接没有写
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


数据修改

  // // 操作数据库
        // String sql = "UPDATE student SET sex=? WHERE name=?";
        // PreparedStatement statement = connection.prepareStatement(sql);
        // statement.setString(1, "男");
        // statement.setString(2, "李四");
        // statement.executeUpdate();
        // System.out.println("修改成功!");
        //
        // // 关闭连接
        // statement.close();
        // connection.close();

数据删除:

// PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM student WHERE id=2");
        // preparedStatement.executeUpdate();
        // System.out.println("删除成功!");
        //
        // // 关闭连接
        // preparedStatement.close();
        // connection.close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值