java基础:使用JDBC连接MySQL数据库,完成增删改查操作

一、MySQL

建表语句

CREATE TABLE `admin` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `username` varchar(25) NOT NULL COMMENT '管理员,姓名',
  `password` varchar(25) NOT NULL COMMENT '登录密码',
  PRIMARY KEY (`id`),
  UNIQUE KEY `UNIQUE` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8

二、JDBC操作

1. 加载JDBC驱动,获取连接

/**
 * JDBC工具类
 *	
 */
//获取jdbc 连接
public class JDBCUtil {
	private Connection conn;
	
	private final String Driver = "com.mysql.jdbc.Driver";
	private final String url = "jdbc:mysql://localhost:3306/myblog";
	private final String username = "root" ;
	private final String password = "123456" ;
	
	public Connection getConnection(){
		
		try {
			Class.forName(Driver);
			conn = DriverManager.getConnection(url, username, password);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return conn;
	}
}

2. 查询

public class BaseDao {
	private JDBCUtil jdbcUtil = new JDBCUtil();
	private Connection conn = null ;
	private PreparedStatement pstmt = null ;
	private ResultSet rs = null ;
	private int executeUpdate = 0 ;
	
	public BaseDao() {
		conn = jdbcUtil.getConnection();
	}
	/*
	 * 查询
	 */
	public void getQuery(){
		String sql = "select * from admin";
		try {
			pstmt= (PreparedStatement) conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()){
				//获取表字段id
				String id = rs.getString("id");
				System.out.println(id);
				//获取表字段 username
				String username = rs.getString("username");
				System.out.println(username);
				//获取表字段 password 
				String password = rs.getString("password");
				System.out.println(password);				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//关闭连接
			try {
				rs.close();
				pstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
		
}

3. 增加

	/*
	 * 增加;
	 * 	表admin ; id主键自增
	 * 	执行excuteUpdate()函数返回int类型,表示受影响的行数;0表示插入失败
	 * 	
	 * 	这么处理可能会抛出异常:如username设置了 唯一索引,若重复插入将抛出异常。
	 * 	需要先进行 查询重复判断;
	 * 
	 */
	public int insert(User user){
		String sql = "insert into admin(id,username,password) value(null,?,?)";
		try {
			pstmt = (PreparedStatement) conn.prepareStatement(sql);
			pstmt.setString(1, user.getUsername());
			pstmt.setString(2, user.getPassword());
			//执行插入更新;返回int类型,表示更新的行数(受影响的行数);无数据更新或者插入返回0
			executeUpdate = pstmt.executeUpdate(); 
			System.out.println("executeUpdate:"+executeUpdate);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				pstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		
		return executeUpdate;
	}

4. 修改

	/*
	 * 修改
	 */
	public int update(User user){
		String sql = "update admin set password = ? where username = ?";
		
		try {
			pstmt = (PreparedStatement) conn.prepareStatement(sql);
			pstmt.setString(1, user.getPassword());
			pstmt.setString(2, user.getUsername());
			executeUpdate = pstmt.executeUpdate();
			System.out.println("executeUpdate:"+executeUpdate);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				pstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return executeUpdate ;
	}

5. 删除

	/*
	 * 删除
	 */
	public int delete(User user){
		String sql = "delete from admin where username = ?";
		try {
			pstmt = (PreparedStatement) conn.prepareStatement(sql);
			pstmt.setString(1, user.getUsername());
			executeUpdate = pstmt.executeUpdate();
			System.out.println("executeUpdate:"+executeUpdate);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				pstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return executeUpdate;
	}

6.Main函数测试

	/*
	 * 测试; 使用时,注销其他操作。避免数据混乱
	 */
	public static void main(String[] args) {
		BaseDao baseDao = new BaseDao();
		User user = new User();
		
		//1. 查询测试
		System.out.println("查询--------------------");
		baseDao.getQuery();
		
		//2. 增加
		System.out.println("插入--------------------");
		user.setUsername("hello1");
		user.setPassword("hello1");
		int insert = baseDao.insert(user);
		if(insert != 0){
			System.out.println("插入数据成功");
		}
		
		//3. 修改
		System.out.println("修改--------------------");
		user.setUsername("hello");
		user.setPassword("main");
		int update  = baseDao.update(user);
		if(update != 0){
			System.out.println("修改数据成功");
		}
		
		//4. 删除
		System.out.println("删除--------------------");
		user.setUsername("qwer");
		int delete = baseDao.delete(user);
		if(delete != 0){
			System.out.println("删除数据成功");
		}
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值