标签:
创建lyyp项目,项目中添加mysql的jar包。
driver=com.mysql.jdbc.Driver dburl=jdbc:mysql://localhost:3306/lyyp_db user=root password=******
//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; } }
// Authors /buddha package entity;//实体类包 // 抽象类 public abstract class IdEntity { protected Long id; public Long getId() { return id; } public void setId(Long id) { this.id = 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 + "]"; } }
// 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; }
// 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(); } }
标签:
原文地址:http://www.cnblogs.com/shakyamuni/p/5117062.html