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
最后:如有雷同,纯属巧合