JDBC链接数据库并实现增删改查功能

1.JDBC简介
在这里插入图片描述
2.JDBC链接数据库基本流程
在这里插入图片描述
3.JDBC链接数据库并实现增删改查功能

package com.qcby.jdbc;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class Jdbc {
static final String  URL  = "jdbc:mysql://localhost:3306/weiyang?useUnicode=true&characterEncoding=utf-8";
static final String ROOT = "root";
static final String PASSWORD = "2020";
public static void main(String[] args) {
	//search();//查询数据
	//insert();//添加数据
	//update();//修改数据
	//delete();//删除数据
}
//数据的查询
public static void search() {
	try {
		// 1.加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		// 2.创建connection对象 链接数据库
		//  url:统一资源定位符 :
		Connection connection = (Connection) DriverManager.getConnection(URL,ROOT,PASSWORD);
		Statement statement = (Statement) connection.createStatement();
		// sql语句
		String sql = "select * from student";
		ResultSet re = statement.executeQuery(sql);
		// 处理数据
		while (re.next()) {
			String idString = re.getString("id");
			String name = re.getString("name");
			String height = re.getString("height");
			String age = re.getString("age");
			System.out.println(idString + " " + name + " " + height + " " + age);
		}
		
	
		if(re !=null) {
			re.close();
		}
		
		if(statement !=null) {
			statement.close();
		}
		
		if (connection !=null) {
			connection.close();
		}
		
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}


// 数据的添加
public static int insert() {
	int temp = 0;
	try {
		Class.forName("com.mysql.jdbc.Driver");
		Connection connection = (Connection) DriverManager.getConnection(URL,ROOT,PASSWORD);
		Statement statement  = (Statement) connection.createStatement();
		String sql = "insert into student(name,height,age) values ('小风',186,23)";
		statement.executeUpdate(sql); 
		temp++;
		
		if(statement !=null) {
			statement.close();
		}
		
		if (connection !=null) {
			connection.close();
		}
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return temp;
}

// 数据的修改
public static int update() {
	int temp = 0;
	try {
		Class.forName("com.mysql.jdbc.Driver");
		Connection connection = (Connection) DriverManager.getConnection(URL,ROOT,PASSWORD);
		Statement statement  = (Statement) connection.createStatement();
		String sql = "update student set name='小黑' ,height = 186,age = 18 where id = 1";
		statement.executeUpdate(sql); 
		temp++;
		
		if(statement !=null) {
			statement.close();
		}
		
		if (connection !=null) {
			connection.close();
		}
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return temp;
}


// 数据的删除
	public static int delete() {
		int temp = 0;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection connection = (Connection) DriverManager.getConnection(URL,ROOT,PASSWORD);
			Statement statement  = (Statement) connection.createStatement();
			String sql = "delete from student where id = 7 ";
			statement.executeUpdate(sql); 
			temp++;
			
			if(statement !=null) {
				statement.close();
			}
			
			if (connection !=null) {
				connection.close();
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return temp;
	}

}

版权声明:本文为lijiaxuan_原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。