当前位置:   article > 正文

Java连接MySQL增删改查_java连接mysql实现增删改查命令

java连接mysql实现增删改查命令

数据库

数据库模型

连接MySQL

public class MySql {
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.cj.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/fzk?serverTimezone=GMT%2B8";
		String name = "root";
		String password = "123456";
		Connection connection = DriverManager.getConnection(url, name, password);
		System.out.println(connection);
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

MySQL增删改查

Statement statement = connection.createStatement();//通过 Connection 得到 Statement 对象
String sql = "INSERT INTO student VALUES ('001', 'fzk', 20)";//MySQL 增  数据语句
int n = statement.executeUpdate(sql);
System.out.println(n);
  • 1
  • 2
  • 3
  • 4

MySQL增加数据

Statement statement = connection.createStatement();//通过 Connection 得到 Statement 对象
String sql = "UPDATE student SET sage = 19 WHERE sno = '001'";//MySQL 改  数据语句
int n = statement.executeUpdate(sql);
System.out.println(n);
  • 1
  • 2
  • 3
  • 4

MySQL修改数据

Statement statement = connection.createStatement();//通过 Connection 得到 Statement 对象
String sql = "DELETE FROM student WHERE sno = '001'";//MySQL 删  数据语句
int n = statement.executeUpdate(sql);
System.out.println(n);
  • 1
  • 2
  • 3
  • 4

在这里删除图片描述

Statement statement = connection.createStatement();//通过 Connection 得到 Statement 对象
ResultSet resultSet = statement.executeQuery("SELECT * FROM student WHERE sno = '001'");
while(resultSet.next()) {
	String sno = resultSet.getString("sno");
	String sname = resultSet.getString("sname");
	int sage = resultSet.getInt("sage");
	System.out.println(sno + ", " + sname + ", " + sage);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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

完整代码

public class MySql {
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		/*
		 * 连接数据库
		 */
		Class.forName("com.mysql.cj.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/fzk?serverTimezone=GMT%2B8";
		String name = "root";
		String password = "123456";
		Connection connection = DriverManager.getConnection(url, name, password);
	
		/*
		 * 数据库增删改查
		 */
		Statement statement = connection.createStatement();//通过 Connection 得到 Statement 对象
		String sql = "INSERT INTO student VALUES ('003', 'kkk', 20)";//MySQL 增  数据语句
		String sql = "UPDATE student SET sage = 19 WHERE sno = '001'";//MySQL 改  数据语句
		String sql = "DELETE FROM student WHERE sno = '001'";//MySQL 删  数据语句
		int n = statement.executeUpdate(sql);
		ResultSet resultSet = statement.executeQuery("SELECT * FROM student WHERE sno = '001'");
		while(resultSet.next()) {
			String sno = resultSet.getString("sno");
			String sname = resultSet.getString("sname");
			int sage = resultSet.getInt("sage");
			System.out.println(sno + ", " + sname + ", " + sage);
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/575450
推荐阅读
相关标签
  

闽ICP备14008679号