Java mysql update(java mysql 更新)

语法说明

update 表名 set 字段名1 = ?, 字段名2 = ? where {条件}

例子如下

package com.pangugle.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TestJDBC {

	public static Connection getConnect() throws ClassNotFoundException, SQLException
	{
		// 通过的反射的方式,导入数据库连接驱动
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://192.168.3.201:3306/testpgdb??useUnicode=true&characterEncoding=utf8&jdbcCompliantTruncation=false&useSSL=false&tinyInt1isBit=false";
		Connection conn = DriverManager.getConnection(url, "root", "root");
		System.out.println("connect mysql is success ? " + !conn.isClosed());
		return conn;
	}

	public static void testInsert(String username, String password) throws ClassNotFoundException, SQLException
	{
		Connection conn = getConnect();

		PreparedStatement ps = null;
		try {
			String sql = "insert into pangugle_user(user_name, user_password) values(?, ?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			ps.setString(2, password);

			int rs = ps.executeUpdate();
			System.out.println("insert 结果为 = " + rs);
		} catch (Exception e) {
			e.printStackTrace();
		} finally
		{
			// 一定要关闭资源(当然我们还有一种技术,叫做连接池技术,就不在需要我们来关闭了)
			ps.close();
			conn.close();
		}
	}



	public static void testDelete(String username) throws ClassNotFoundException, SQLException
	{
		Connection conn = getConnect();

		PreparedStatement ps = null;
		try {
			String sql = "delete from pangugle_user where user_name = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);

			int rs = ps.executeUpdate();
			System.out.println("delete 结果为 = " + rs);
		} catch (Exception e) {
			e.printStackTrace();
		} finally
		{
			// 一定要关闭资源(当然我们还有一种技术,叫做连接池技术,就不在需要我们来关闭了)
			ps.close();
			conn.close();
		}
	}

	public static void testDeleteAll() throws ClassNotFoundException, SQLException
	{
		Connection conn = getConnect();

		PreparedStatement ps = null;
		try {
			String sql = "delete from pangugle_user";
			ps = conn.prepareStatement(sql);

			int rs = ps.executeUpdate();
			System.out.println("delete 结果为 = " + rs);
		} catch (Exception e) {
			e.printStackTrace();
		} finally
		{
			// 一定要关闭资源(当然我们还有一种技术,叫做连接池技术,就不在需要我们来关闭了)
			ps.close();
			conn.close();
		}
	}

	public static void testUpdate(String username, String password) throws ClassNotFoundException, SQLException
	{
		Connection conn = getConnect();

		PreparedStatement ps = null;
		try {
			String sql = "update pangugle_user set user_password = ? where user_name = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, password);
			ps.setString(2, username);

			int rs = ps.executeUpdate();
			System.out.println("update 结果为 = " + rs);
		} catch (Exception e) {
			e.printStackTrace();
		} finally
		{
			// 一定要关闭资源(当然我们还有一种技术,叫做连接池技术,就不在需要我们来关闭了)
			ps.close();
			conn.close();
		}
	}

	public static void main(String[] args) throws ClassNotFoundException, SQLException
	{
		// getConnect();

		//testInsert("u1", "p1");
		//testInsert("u2", "p2");
		//testInsert("u3", "p3");

		//testDelete("u1");

		testUpdate("u2", "p2222");
	}


}

运行结果为

connect mysql is success ? true
update 结果为 = 1

查看数据库,结果如下

user_id user_name user_passowrd
3 u2 p2222
4 u3 p3

说明数据已被更新成功