MySql 执行JDBC联接(增/删/改/查)操作

查看原文和视频地址:【MySql】执行JDBC联接(增/删/改/查)操作


新建BaseDao

package cn.wingfly.dao;

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

public class BaseDao {
	Connection con = null;
	Statement st = null;
	ResultSet rs = null;

	/**
	 * 获得联接
	 * 
	 * @return
	 */
	public Connection getConnection() {
		try {
			// 加载驱动,这一句也可写为:Class.forName("com.mysql.jdbc.Driver"); 
			Class.forName("com.mysql.jdbc.Driver").newInstance();

			// 建立到MySQL的连接 
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/money_note?characterEncoding=UTF-8", "root", "root");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}

	/**
	 * 关闭数据源
	 */
	public void CloseConnection(Connection con, Statement s, ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
			}
			if (s != null) {
				s.close();
			}
			if (con != null) {
				con.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

测试类

package cn.wingfly.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class Test extends BaseDao {

	Connection con = null;
	Statement st = null;
	ResultSet rs = null;

	/**
	 * 查询数据
	 */
	public void find() {
		con = getConnection();	// 获得联接
		try {
			st = con.createStatement();
			rs = st.executeQuery("select * from app_user");
			while (rs.next()) {
				System.out.println("编号:" + rs.getInt("uuid") + ", 姓名:" + rs.getString("userName") + ", 性别:" + rs.getString("sex") + ", 生日:" + rs.getString("birthday") + ", 住址:" + rs.getString("address"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			CloseConnection(con, st, rs);	// 关闭联接
		}
	}

	/**
	 * 添加数据
	 */
	public void add() {
		con = getConnection();
		try {
			st = con.createStatement();
			int result = st.executeUpdate("insert into app_user(userName,passWord,sex,birthday,address) values('赵丽颖','wanying','女','1992-02-03','北京市')");
			if (result > 0) {
				System.out.println("插入成功");
			} else {
				System.out.println("插入失败");
			}
		} catch (SQLException e) {
			System.out.println("插入失败");
			e.printStackTrace();
		} finally {
			CloseConnection(con, st, rs);
		}
	}

	/**
	 * 更新数据
	 */
	public void update() {
		con = getConnection();
		try {
			st = con.createStatement();
			int result = st.executeUpdate("update app_user set address = '河南' where uuid = '3'");
			if (result > 0) {
				System.out.println("更新成功");
			} else {
				System.out.println("更新失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			CloseConnection(con, st, rs);
		}
	}

	/**
	 * 删除数据
	 */
	public void delete() {
		con = getConnection();
		try {
			st = con.createStatement();
			int result = st.executeUpdate("delete from app_user where uuid = '3'");
			if (result > 0) {
				System.out.println("删除成功");
			} else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			CloseConnection(con, st, rs);
		}
	}

	public static void main(String[] args) {
		Test test = new Test();
		//test.add();
		// test.update();
		 test.delete();
		test.find();

	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值