0
点赞
收藏
分享

微信扫一扫

Java连接数据库中的MysqlUtil.java增删改查详解

三分梦_0bc3 2022-02-15 阅读 77

目录

举一个查询类GetCount.java

 增删改查的MysqlUtil.java


四个类

流程

举一个查询类GetCount.java

这个是连接数据库的代码 (需要改成自己的)

static String url = "jdbc:mysql://localhost:3306/car?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
	static String user = "账号";
	static String password = "密码";
package com.sql;

import java.sql.DriverManager;
import java.sql.ResultSet;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;



public class GetCount {

	static String url = "jdbc:mysql://localhost:3306/car?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
	static String user = "账号";
	static String password = "密码";
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		getCount();
	}
	
	public static void getCount() {
		int count = 0;
		try {
			// 1. 加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			// 2.创建链接
			Connection connection = (Connection) DriverManager.getConnection(url,user,password);
			// 3.创建Statement对象
			Statement statement = (Statement) connection.createStatement();
			// 4.执行sql
			String sql = "select  * from student";
			ResultSet resultSet = statement.executeQuery(sql);
			// 5. 结果
			while (resultSet.next()) {
				count += 1;
			}
			
			if (resultSet !=null) {
				resultSet.close();
			}
			
			if (statement !=null) {
				statement.close();
			}
			
			if (connection !=null) {
				connection.close();
			}
			
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(count); 
	}

}

七条数据:

 增删改查的MysqlUtil.java

 

package com.sql;


import java.sql.DriverManager;
import java.sql.ResultSet;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class MysqlUtil {

	static String url = "jdbc:mysql://localhost:3306/car?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
	static String user = "账号";
	static String password = "密码";
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		delete();
	}
	/*数据查询
	 * 
	 * 
	 */
	public static void search() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			// 2.创建链接
			Connection connection = (Connection) DriverManager.getConnection(url,user,password);
						// 3.创建Statement对象
			Statement statement = (Statement) connection.createStatement();
						// 4.执行sql
			String sql = "select * from student";
			ResultSet resultSet = statement.executeQuery(sql);
			while(resultSet.next()) {
				String id = resultSet.getString("id");
				String name = resultSet.getString("name");
				String idCard = resultSet.getString("idCard");
				String phone = resultSet.getString("phone");
				System.out.println(id + " " + name + " " + idCard + " " + phone);
				
			}
			if(resultSet!=null) {
				resultSet.close();
			}
			if (statement !=null) {
				statement.close();	
			}
			if (connection !=null) {
				connection.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/*数据插入
	 * 
	 * 
	 * 
	 * */
	private static int insert() {
		// TODO Auto-generated method stub
		int i =0;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection connection =(Connection) DriverManager.getConnection(url,user,password);
//			创建Statement对象
			Statement statement =(Statement) connection.createStatement();
			String sql ="insert into student(name,idCard,phone,height) values('苹果','12346','123456','184')";
			statement.executeUpdate(sql);
			
			if (statement !=null) {
				statement.close();	
			}
			if (connection !=null) {
				connection.close();
			}
			i=1;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 1;
	}
	
	public static int update() {
		int  i = 0;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection connection = (Connection) DriverManager.getConnection(url,user,password);
			// 3.创建Statement对象
			Statement statement = (Statement) connection.createStatement();
			String sql = "update student set name = '香蕉'  where id =  1" ;
			statement.executeUpdate(sql);
			if (statement !=null) {
				statement.close();
			}
			
			if (connection !=null) {
				connection.close();
			}
			
			i = 1;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}
	
	
	public static int delete() {
		int i=0;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection connection = (Connection) DriverManager.getConnection(url,user,password);
			// 3.创建Statement对象
			Statement statement = (Statement) connection.createStatement();
			String sql = "delete from student where id = 1" ;
			statement.executeUpdate(sql);
			if (statement !=null) {
				statement.close();
			}
			
			if (connection !=null) {
				connection.close();
			}
			
			i = 1;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return i ;
		
	}
	
}

在main()中调用

 

举报

相关推荐

0 条评论