Java中普通连接数据库的方法

Java中用普通方法连接数据库

示例

1.mysql.properties 文件:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/product?useSSL=false&serverTimezone=UTC&characterEncoding=utf8&useUnicode=true
username=root
password=123456

2.util 类(工具类):

public class MySQLUtils {
	private static String driver;
	private static String url;
	private static String username;
	private static String password;
	static {
		try {
			Properties properties = new Properties();
			URL fileUrl = MySQLUtils.class.getResource("/mysql.properties");
			String file = fileUrl.getFile();
			properties.load(new FileReader(file));
			driver = properties.getProperty("driver");
			url = properties.getProperty("url");
			username = properties.getProperty("username");
			password= properties.getProperty("password");
			Class.forName(driver);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("connection mysql failed!");
		}
	}

	/**
	 * 获取数据库链接方法
	 * @return	返回链接对象
	 * @throws SQLException
	 */
	public static Connection getConnection() throws SQLException {
		return DriverManager.getConnection(url, username, password);
	}
	/**
	 * 释放资源
	 * @param rs,ResultSet
	 * @param stmt,Statement
	 * @param conn,Connection
	 */
	public static void free(ResultSet rs,Statement stmt,Connection conn) {
		try {
			if(rs!=null) {
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(stmt!=null) {
					stmt.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				try {
					if(conn!=null) {
						conn.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

3.dao 层:

/**
 * 订单主表 模块,Dao接口设计
 * @author Administrator
 *
 */
public interface OrderMasterDao {
	//收货人新增地址
	public boolean insertNewAddress(UserAddress userAddress) throws SQLException;

	//通过id查询地址
	public UserAddress findUserAddressById(int id) throws SQLException;
	
	//通过id修改地址
	public boolean updateUserAddressById(UserAddress userAddress) throws SQLException;
	
	//通过id删除地址
	public boolean deleteUserAddressById(int id) throws SQLException;
}

4.dao 层的实现:

/**
 * 订单模块Dao实现
 * @author Administrator
 */
public class OrderMasterDaoImpl implements OrderMasterDao {

	@Override
	public boolean insertNewAddress(UserAddress userAddress) throws SQLException {// 收货人新增地址
		// sql语句
		String sql = "insert into user_address(addr_name,addr_phone,province,city,detail) values(?,?,?,?,?)";
		// 获取连接
		Connection conn = MySQLUtils.getConnection();
		// 获取Connection对象
		PreparedStatement ps = conn.prepareStatement(sql);
		// 处理数据
		ps.setString(1, userAddress.getAddrName());
		ps.setString(2, userAddress.getAddrPhone());
		ps.setString(3, userAddress.getProvince());
		ps.setString(4, userAddress.getCity());
		ps.setString(5, userAddress.getDetail());

		int res = ps.executeUpdate();
		// 释放资源
		MySQLUtils.free(null, ps, conn);
		return res == 1 ? true : false;
	}

	@Override
	public UserAddress findUserAddressById(int id) throws SQLException {// 通过id查询地址
		UserAddress userAddress = null;
		// sql语句
		String sql = "select id,addr_name,addr_phone,province,city,detail from user_address where id=?";
		// 获取连接
		Connection conn = MySQLUtils.getConnection();
		// 获取Connection对象
		PreparedStatement ps = conn.prepareStatement(sql);
		// 获取id,addr_name
		ps.setInt(1, id);
		ResultSet rs = ps.executeQuery();
		// 处理数据
		while (rs.next()) {
			int id1 = rs.getInt("id");
			String addrName1 = rs.getString("addr_name");
			String addrPhone = rs.getString("addr_phone");
			String province = rs.getString("province");
			String city = rs.getString("city");
			String detail = rs.getString("detail");

			userAddress = new UserAddress(id1, addrName1, addrPhone, province, city, detail);
		}
		// 释放资源
		MySQLUtils.free(rs, ps, conn);
		return userAddress;
	}

	@Override
	public boolean updateUserAddressById(UserAddress userAddress) throws SQLException {// 通过id修改地址
		// TODO Auto-generated method stub
		// sql语句
		String sql = "update user_address set addr_name=?,addr_phone=?,province=?,city=?,detail=? where id=?";

		// 获取连接
		Connection conn = MySQLUtils.getConnection();
		// 获取Connection对象
		PreparedStatement ps = conn.prepareStatement(sql);
		// 处理数据
		ps.setString(1, userAddress.getAddrName());
		ps.setString(2, userAddress.getAddrPhone());
		ps.setString(3, userAddress.getProvince());
		ps.setString(4, userAddress.getCity());
		ps.setString(5, userAddress.getDetail());
		
		ps.setInt(6, userAddress.getId());

		int res = ps.executeUpdate();
		// 释放资源
		MySQLUtils.free(null, ps, conn);
		return res == 1 ? true : false;
	}

	@Override
	public boolean deleteUserAddressById(int id) throws SQLException {// 通过id删除地址
		// sql语句
		String sql = "delete from user_address where id=?";
		// 获取连接
		Connection conn = MySQLUtils.getConnection();
		// 获取Connection对象
		PreparedStatement ps = conn.prepareStatement(sql);

		// 通过order_id来处理数据
		ps.setInt(1, id);

		int res = ps.executeUpdate();
		// 释放资源
		MySQLUtils.free(null, ps, conn);
		return res == 1 ? true : false;
	}
}

用到的jar包:
百度网盘:
链接:https://pan.baidu.com/s/1icrtMjA9en_jTwPyru3kPw
提取码:oue4

最后:如有雷同,纯属巧合

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值