eclipse connects to MySQL and performs additions, deletions, and modifications

First create a database and database table in MySQL:

CREATE DATABASE demo01 CHARSET utf8;
CREATE TABLE demo01(
	d_id INT PRIMARY KEY AUTO_INCREMENT,
	d_name VARCHAR(30),
	hobby VARCHAR(30)
);

As shown in SQLyog, it has been created:
sql
Then open eclipse to create a new Java project and create a demo class:

package day05;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Scanner;

/**
 * ctrl + n 快速创建项目
 * ctrl + + 调节字体
 * @author Administrator
 *	1、导包
 *  2、加载驱动
 *  3、创建连接对象
 *  4、编写sql创建sql编译器
 *  5、执行sql返回结果集
 *  6、释放资源
 */
public class Demo01 {
    
    
	public static void main(String[] args) throws Exception  {
    
    
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入姓名和爱好:");
		//nextLine键盘录入字符串
		String uname = sc.nextLine();
		String hobby = sc.nextLine();
		//1、加载驱动com.mysql.jdbc.Driver
		Class.forName("com.mysql.jdbc.Driver");
		String user = "root";
		String password = "root";
		String url = "jdbc:mysql://localhost:3306/db1?characterEncoding=utf8";
		//2、获取连接对象   ctrl + 1
		Connection conn = DriverManager.getConnection(url, user, password);
		//3、通过连接对象conn去创建sql编译器
		Statement stat = conn.createStatement();
		//4、编写sqlinsert into user values(null,'张三','吃饭');
		String sql = "insert into user values(null,'" + uname + "','" + hobby + "')";
		//5、执行sql返回结果集  i代表受影响的行数
		int i = stat.executeUpdate(sql);
		if (i > 0) {
    
    
			System.out.println("添加成功");
		} else {
    
    
			System.out.println("添加失败");

		}
		//6、释放资源
		stat.close();
		conn.close();
	}
}

The results of the operation are shown in the figure:
Insert picture description here
View data: (Here we create a new class)

package day05;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 查询表中所有的数据
 * @author Administrator
 *
 */
public class Demo02 {
    
    
	public static void main(String[] args) throws Exception {
    
    
		//1、加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		String user = "root";
		String password = "root";
		String url = "jdbc:mysql://localhost:3306/db1?characterEncoding=utf8";
		//2、获取连接对象
		Connection conn = DriverManager.getConnection(url, user, password);
		//3、编写sql
		String sql = "select * from user";
		//4、创建sql执行器
		Statement stat = conn.createStatement();
		//5、执行sql
		/**
		 * 增删改DML   executeUpdate方法      int i  代表受影响的行数
		 * 查询DQL    executeQuery方法     ResultSet 是一个特殊的集合
		 */
		ResultSet rs = stat.executeQuery(sql);
		/**
		 * rs.next()方法     判断是否有下一条数据    有返回true,没有返回false
		 * rs.getXXX(序号) 获取指定序号的列的值
		 */
		while(rs.next()) {
    
    
			System.out.println(rs.getInt(1) + "--" + rs.getString(2) 
			+ "----" +  rs.getString(3));
		}
		//6、释放资源     先开后关
		rs.close();
		stat.close();
		conn.close();
		
		//TODO  查询功能     删除和修改
		
	}
}

The result is shown in the figure:
Insert picture description here
Modify the data:

package day05;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class demo02 {
    
    
	public static void main(String[] args) throws Exception {
    
    
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/demo01?characterEncoding=utf8";
		
		String user = "root";
		
		String password = "root";
		Connection conn = DriverManager.getConnection(url , user , password );
		Statement stat = conn.createStatement();
		String sql = "update demo01 set `hobby`='不讲武德' where `d_name` = '马保国'" ;
		int i = stat.executeUpdate(sql);
		if (i>0) {
    
    
			System.out.println("修改成功!");
		}
		else {
    
    
			System.out.println("修改失败!");
		}
	}
}

View Results:
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46202060/article/details/110930420