JDBC的基本操作

package com.lxf.domain:

package com.lxf.domain;

import java.util.Date;

public class User {
private int id;
private String name;
private Date birthday;
private float money;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public float getMoney() {
return money;
}
public void setMoney(float money) {
this.money = money;
}
public String toString(){
String information = getId()+" "+getName()+" "+getBirthday()+" "+getMoney();
return information;
}
}


package com.lxf.dao:
异常类:
package com.lxf.dao;

public class DaoException extends RuntimeException{
private static final long serialVersionUID = 1L;
public DaoException(){}
public DaoException(String message){
super(message);
}
public DaoException(Throwable cause){
super(cause);
}
public DaoException(String message,Throwable cause){
super(message,cause);
}
}


工厂模式工厂类:
package com.lxf.dao;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class DaoFactory {
private static UserDao userDao = null;
private static DaoFactory instance = new DaoFactory();

private DaoFactory() {
try (InputStream inStream = DaoFactory.class.getClassLoader()
.getResourceAsStream("daoconfig.properties")) {

Properties prop = new Properties();
prop.load(inStream);

String userDaoClass = prop.getProperty("userDaoClass");
userDao = (UserDao) Class.forName(userDaoClass).newInstance();

} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | IOException e) {
throw new ExceptionInInitializerError(e);
}
}

public static DaoFactory getInstance() {
return instance;
}

public UserDao getUserDao() {
return userDao;
}
}

工厂类的daoconfig.properties文件:
userDaoClass=com.lxf.dao.impl.UserDaoJdbcImpl


接口:
package com.lxf.dao;

import com.lxf.domain.User;

public interface UserDao {
//增加用户
public void addUser(User user);
//根据Id获取用户
public User getUser(int userId);
//根据用户名获取用户
public User findUser(String loginName);
//更新用户
public void update(User user);
//删除用户
public boolean delete(User user);
}



package com.lxf.dao.impl:
接口的实现:
package com.lxf.dao.impl;

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

import com.lxf.dao.DaoException;
import com.lxf.dao.UserDao;
import com.lxf.domain.User;
import com.lxf.jdbc.JdbcUtils;

public class UserDaoJdbcImpl implements UserDao {

@Override
public void addUser(User user) {
String sql = "insert into user(name,birthday,money)values(?,?,?)";
try (Connection conn = JdbcUtils.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, user.getName());
ps.setDate(2, new java.sql.Date(user.getBirthday().getTime()));
ps.setFloat(3, user.getMoney());
ps.executeUpdate();
} catch (SQLException e) {
throw new DaoException(e.getMessage(), e);
}
}

@Override
public User getUser(int userId) {
User user = null;
String sql = "select id,name,birthday,money from user where id=?";
try (PreparedStatement ps = JdbcUtils.getConnection().prepareStatement(
sql);) {
// 处理结果
ps.setInt(1, userId);
try (ResultSet rs = ps.executeQuery();) {
while (rs.next()) {
user = mappingUser(rs);
}
} catch (SQLException e) {
throw new DaoException(e.getMessage(), e);
}
} catch (SQLException e) {
throw new DaoException(e.getMessage(), e);
}
return user;
}

@Override
public User findUser(String loginName) {
User user = null;
String sql = "select id,name,birthday,money from user where name=?";
try (PreparedStatement ps = JdbcUtils.getConnection().prepareStatement(
sql);) {
ps.setString(1, loginName);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
user = mappingUser(rs);
}
} catch (SQLException e) {
throw new DaoException(e.getMessage(), e);
}
} catch (SQLException e) {
throw new DaoException(e.getMessage(), e);
}
return user;
}

@Override
public void update(User user) {
String sql = "update user set name=?,birthday=?,money=? where id=?";
try (PreparedStatement ps = JdbcUtils.getConnection().prepareStatement(
sql);) {
ps.setString(1, user.getName());
ps.setDate(2, new java.sql.Date(user.getBirthday().getTime()));
ps.setFloat(3, user.getMoney());
ps.setInt(4, user.getId());
ps.executeUpdate();// 执行语句,返回受影响的行数
} catch (Exception e) {
throw new DaoException(e.getMessage(), e);
}
}

@Override
public boolean delete(User user) {
boolean flag = false;
String sql = "delete from user where id=" + user.getId();
try (Connection conn = JdbcUtils.getConnection();
Statement st = conn.createStatement();) {
int i = st.executeUpdate(sql); // 执行语句,返回受影响的行数
if (i >= 0) {
flag = true;
}
} catch (SQLException e) {
throw new DaoException(e.getMessage(), e);
}
return flag;
}

private User mappingUser(ResultSet rs) throws SQLException {
User user;
user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setBirthday(rs.getDate("birthday"));
user.setMoney(rs.getFloat("money"));
return user;
}
}

一个简单的测试类:
package com.lxf.dao;

import java.util.Date;

import com.lxf.dao.impl.UserDaoJdbcImpl;
import com.lxf.domain.User;

public class UserDaoTest {
public static void main(String[]args){
UserDao userDao = DaoFactory.getInstance().getUserDao();
sop(userDao.getUser(1));
}
public static void sop(Object obj){
System.out.println(obj);
}
}

[b][color=blue]代码有点基础,贴出来怕自己忘记,没有贴出数据库连接代码。在上一篇博客。
如果发现有需要更改的地方,请及时指出,我不胜感激。[/color][/b]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值