JDBC.java

 DBConnection

package com.sql;

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

public class DBConnection {

	public static void main(String[] args) {

	}

	String driver = "com.mysql.jdbc.Driver";
	String url = "jdbc:mysql://localhost:3306/commodity?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
	String user = "root";
	String password = "2020";

	public Connection conn;

	public DBConnection() {

		try {
			// 加载驱动jar包
			Class.forName(driver);   // 反射:获取类的信息
			conn = (Connection) DriverManager.getConnection(url, user, password);

			// if(!conn.isClosed())
			// System.out.println("Succeeded connecting to the Database!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void close() {
		try {
			this.conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

MySqlUtil

package com.sql;

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

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 = "root";
	static String password = "2020";
	
	public static void main(String[] args) {
		 delete();
	}
	
	/**
	 * 	数据的查询
	 */
	public static void search() {
		// 1. 加载驱动
		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();
		}
		
	}
	
	/**
	 * 数据的插入
	 * @return
	 */
	public static int insert() {
		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 = "insert into student (name,idCard,phone,height) values ('是是是','57892','1289312650','180')";
			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  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 = 5" ;
			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;
	}
	
	
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值