创建lyyp项目,项目中添加mysql的jar包。
mysql-connector-java-5.1.7-bin.jar 此包放入项目WebContent-WEB-INF-lib中,
项目右键选择Build Path-Java Build Path - Libraries,
Add JARs...找到项目中在上一步引入的jar包,OK.
Java Resources -src 创建dbconfig.properties文件
代码:
driver=com.mysql.jdbc.Driver dburl=jdbc:mysql://localhost:3306/lyyp_db user=root password=******
Java Resources -src 创建系统通用工具类
Package【包名】:util
Name【类名】:ConnectionFactory
代码:
//Authors /buddha package util; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.util.Properties; public class ConnectionFactory { private static String driver;// 数据库加载驱动 private static String dburl;// 链接地址 private static String user;// 用户名 private static String password;// 密码 //私有 静态 连接工厂 private static final ConnectionFactory factory = new ConnectionFactory(); private Connection conn;//连接 // 配置信息的读取代码 static { Properties prop = new Properties();// 定义类,处理键值对 try { // 获取配置文件【dbconfig.properties】 // 获取属性文件中的内容,首先获取类加载器,然后读取属性文件中的内容,从输入流中读取键值对列表 //输入流=连接工厂.类.得到类装载器.获取资源作为流("资源名") InputStream in = ConnectionFactory.class.getClassLoader().getResourceAsStream("dbconfig.properties"); prop.load(in); } catch (Exception e) { System.out.println("===配置文件读取错误==="); } driver = prop.getProperty("driver"); dburl = prop.getProperty("dburl"); user = prop.getProperty("user"); password = prop.getProperty("password"); } // 默认的构造函数 private ConnectionFactory() { } public static ConnectionFactory getInstance(){ return factory; } //获取数据库的连接方法 public Connection makeConnection(){ try { Class.forName("driver"); conn=DriverManager.getConnection(dburl,user,password); } catch (Exception e) { e.printStackTrace(); } return conn; } }
Java Resources - src 创建 实体抽象类
Package【包名】:entity
Name【类名】:IdEntity
代码:
// Authors /buddha package entity;//实体类包 // 抽象类 public abstract class IdEntity { protected Long id; public Long getId() { return id; } public void setId(Long id) { this.id = id; } }
entity包下 创建user类
Name【类名】:User
Browse...输入IdEntity选中,
声明字段之后,Source - Generate Getters Setters...【创建get,set方法】
Source - Generte toString()...【】
除了原有勾选,添加 Inherited fields - id 勾选。
代码:
// Authors /buddha package entity;//实体类包 //user 继承自IdEntity抽象实体类 public class User extends IdEntity { private String uname; private String upassword; //get set方法 public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpassword() { return upassword; } public void setUpassword(String upassword) { this.upassword = upassword; } @Override public String toString() { return "User [uname=" + uname + ", upassword=" + upassword + ", id=" + id + "]"; } }
Java Resources -src 创建接口
Package【包名】:dao
Name【接口名】:UserDao
代码:
// Authors /buddha package dao;//接口包 import java.sql.Connection; import java.sql.SQLException; import entity.User; //user接口 public interface UserDao { //保存 public void save(Connection conn, User user) throws SQLException; //更新 public void update(Connection conn, Long id, User user) throws SQLException; //删除 public void delete(Connection conn, User user) throws SQLException; }
dao包下创建一个接口具体实现类包
Package【包名】:dao.impl 【接口具体实现类】
Name【类名】:UserDaoImpl
Add...【添加】 UserDao接口
代码:
// Authors /buddha package dao.impl;//包名【接口具体实现类包】 import java.sql.Connection; import java.sql.SQLException; import java.sql.PreparedStatement;//这里引入的包注意更改 import dao.UserDao; import entity.User; //接口具体实现类 public class UserDaoImpl implements UserDao { // 保存用户信息 @Override public void save(Connection conn, User user) throws SQLException { PreparedStatement ps = conn.prepareCall("INSERT INTO lyyp_common_user(UName,UPassword) VALUES(?,?)"); ps.setString(1, user.getUname()); ps.setString(2, user.getUpassword()); ps.execute(); } // 根据用户指定的ID更新用户信息 @Override public void update(Connection conn, Long id, User user) throws SQLException { String updateSql = "UPDATE lyyp_common_user SET UName = ?,UPassword = ? WHERE id = ?"; PreparedStatement ps = conn.prepareStatement(updateSql); ps.setString(1, user.getUname()); ps.setString(2, user.getUpassword()); ps.setLong(3, id); ps.execute(); } // 删除指定的用户信息 @Override public void delete(Connection conn, User user) throws SQLException { PreparedStatement ps = conn.prepareStatement("DELETE FROM lyyp_common_user WHERE id = ?"); ps.setLong(1, user.getId()); ps.execute(); } }
时间: 2024-10-18 21:05:05