05-java对MySQL数据库的增删改查

路转峰回出画塘,一山枫叶背残阳。——《浣溪沙》

前一篇,是我第一次用java连接mysql数据库。这一篇,主要介绍怎样在java中使用SQL语句,对数据库进行“增删改查“这四个操作,也算是对MySQL数据库的深入学习。

  1. SQL语法
  2. 常用功能类(JDBCUtil)
  3. 专门用来做“增删改查”的类(AddDeleteUpdateShow)
  4. connection.createStatement()和connection.preparedStatement(sql)的比较。
  5. 总结

一.SQL语法

假设我现在有一张表(LoginTable),这张表是用户登录账号密码的记录,有 账号(zhanghao)和密码(password)

  1. 增加数据到数据库(添加新的账号和密码)
insert into LoginTable(zhanghao,password) values(?,?)
  1. 删除数据库中相关数据(删除指定账号)
delete from LoginTable where zhanghao=?
  1. 修改数据库中的数据(修改指定账号的密码)
update LoginTable set password=? where zhanghao=?
  1. 查看数据库中的数据(*代表所有)
select * from LoginTable

二. 常用功能类(JDBCUtil)

因为java要操作MySQL数据库首先得建立连接,所以我们建立一个专门用来获取连接和关闭连接的专用功能类(JDBCUtil)

package Login2;

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


public class JDBCUtil {
	public static Connection getconnection() {
		String connetURL="jdbc:mysql://127.0.0.1:3306/test?useSSL=false";
		String connetZHANGHAO="root";
		String connetPASSWORD="root";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection connection=DriverManager.getConnection(connetURL,connetZHANGHAO,connetPASSWORD);
			return connection;
		}catch (Exception e) {
			System.out.println("数据库断开连接");
		}
		return null;
	}
	public static void closeOpen(ResultSet resultSet,PreparedStatement preparedStatement,Connection connection) {
		if(resultSet!=null)
			try {
				resultSet.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		if(preparedStatement!=null)
			try {
				preparedStatement.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if(connection!=null)
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
}

三.专门用来做“增删改查”的类(AddDeleteUpdateShow)

package Login2;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class AddDeleteUpdaeShow {
	public static void add(String zhanghao,String password) {
		Connection connection=JDBCUtil.getconnection();
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		int flag=0;
		String sql="insert into mountpasswordtable(zhanghao,password) values(?,?)";
		try {
			 preparedStatement=connection.prepareStatement(sql);
			preparedStatement.setString(1, zhanghao);
			preparedStatement.setString(2, password);
			 flag=preparedStatement.executeUpdate();
			if(flag==1) {
				System.out.println("sucess");
			}else {
				System.out.println("fail");
			}
		} catch (Exception e) {
			System.out.println("error:");
			System.out.println("账号重复");
		}finally {
			JDBCUtil.closeOpen(resultSet, preparedStatement, connection);
		}
		
	}
	public static void delete(String zhanghao) {
		Connection connection=null;
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		try {
			connection=JDBCUtil.getconnection();
			String sql="delete from mountpasswordtable where zhanghao=?";
			preparedStatement=connection.prepareStatement(sql);
			preparedStatement.setString(1, zhanghao);
			int number=preparedStatement.executeUpdate();
			if(number>0) {
				System.out.println("sucess");
			}else {
				System.out.println("fail");
			}
		} catch (Exception e) {
			System.out.println("error");
		}finally {
			JDBCUtil.closeOpen(resultSet, preparedStatement, connection);
		}
	}
	public static void update(String zhanghao,String password) {
		Connection connection=null;
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		try {
			connection=JDBCUtil.getconnection();
			String sql="update mountpasswordtable set password=? where zhanghao=?";
			preparedStatement=connection.prepareStatement(sql);
			preparedStatement.setString(1, password);//这里的setString是?的值
			preparedStatement.setString(2, zhanghao);
			int number=preparedStatement.executeUpdate();
			if(number>0) {
				System.out.println("sucess");
			}else {
				System.out.println("fail");
			}
		} catch (Exception e) {
			System.out.println("error");
		}finally {
			JDBCUtil.closeOpen(resultSet, preparedStatement, connection);
		}
		
	}
	public static void show() {
		Connection connection=null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet=null;
		try {
			connection=JDBCUtil.getconnection();
			String sql="select * from mountpasswordtable";
			preparedStatement=connection.prepareStatement(sql);
			resultSet=preparedStatement.executeQuery();
			while(resultSet.next()) {
				System.out.println(resultSet.getInt(1)+"/"+resultSet.getString(2)+"/"+resultSet.getString(3));
			}
			
		} catch (Exception e) {
			System.out.println("error");
		}finally {
			JDBCUtil.closeOpen(resultSet, preparedStatement, connection);
		}
	}

}

四.connection.createStatement()和connection.preparedStatement(sql)的比较。

  • 对于connection.createStatement(),它的SQL语句是下面这样的。这使得别人填账号和密码时,即使密码错误也能登录,比如说,某人在密码框中填入"123’ or ‘1’='1"。这个密码or后面的等式1=1成立,所以就使得该密码正确,从而即便不知道密码也能登录。存在安全漏洞。
Class.forName("com.mysql.jdbc.Driver");
			connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useSSL=false", "root", "root");
			statement=connection.createStatement();
			resultSet=statement.executeQuery("select * from logininfo where zhanghao='"+zhanghao+"' and password='"+password+"'");
  • 对于 connection.preparedStatement(sql),代码如下方显示。其实它主动权在MySQL数据库了,它会去寻找数据库中是否存在该账号和密码,从而使得即便你在密码框写"123’ or ‘1’='1",它也仅仅会把这个当成一个密码,而不会因为等式成立就判断你密码正确。所以一般账号密码登录的,采取这种安全写法
Class.forName("com.mysql.jdbc.Driver");
				connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useSSL=false", "root", "root");
				preparedStatement=statement=connection.prepareStatement("select * from logininfo where zhanghao=? and password=?");
				preparedStatement.setString(1, zhanghao);
				preparedStatement.setString(2, password);
				resultSet=preparedStatement.executeQuery();

五.总结

JDBCUtil这种专门建立一个常用类的思想很好,经过封装,我们就能简单调用,减少了代码冗余,也使得代码整洁易懂。对数据库的增删改查学习,是对JDBC连接数据库的深入,为接下来JavaWeb学习打下一个基础。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值