标签:mys dbus throw 方法 interface job code ntp sys
背景
业务分析
实例
功能
项目结构
项目代码
1 package dbc; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.SQLException; 6 7 public class DatabaseConnection { 8 private static final String DBDRIVER = "com.mysql.cj.jdbc.Driver"; 9 private static final String DBURL = "jdbc:mysql://127.0.0.1:3306/company?serverTimezone=GMT%2B8&characterEncoding=UTF-8&useSSL=false"; 10 private static final String DBUSER = "root"; 11 private static final String PASSWORD = "Chen1227+"; 12 private Connection conn = null; 13 14 // 连接数据库 15 public DatabaseConnection() { 16 try { 17 Class.forName(DBDRIVER); 18 } catch (ClassNotFoundException e1) { 19 e1.printStackTrace(); 20 } 21 try { 22 this.conn = DriverManager.getConnection(DBURL,DBUSER,PASSWORD); 23 } catch (SQLException e) { 24 e.printStackTrace(); 25 } 26 } 27 28 // 取得数据库的连接对象 29 public Connection getConnection() { 30 return this.conn; 31 } 32 33 // 关闭数据库 34 public void close() { 35 if(this.conn != null) { 36 try { 37 this.conn.close(); 38 //System.out.print("close success!"); 39 } catch (SQLException e) { 40 e.printStackTrace(); 41 } 42 } 43 } 44 }
1 package vo; 2 3 import java.io.Serializable; 4 import java.util.Date; 5 6 7 @SuppressWarnings("serial") 8 public class Emp implements Serializable{ 9 private Integer empno; 10 private String ename; 11 private String job; 12 private Date hiredate; 13 private Double sal; 14 private Double comm; 15 16 public Integer getEmpno() { 17 return empno; 18 } 19 public void setEmpno(Integer empno) { 20 this.empno = empno; 21 } 22 public String getEname() { 23 return ename; 24 } 25 public void setEname(String name) { 26 this.ename = name; 27 } 28 public String getJob() { 29 return job; 30 } 31 public void setJob(String job) { 32 this.job = job; 33 } 34 public Date getHiredate() { 35 return hiredate; 36 } 37 public void setHiredate(Date hiredate) { 38 this.hiredate = hiredate; 39 } 40 public Double getSal() { 41 return sal; 42 } 43 public void setSal(Double sal) { 44 this.sal = sal; 45 } 46 public Double getComm() { 47 return comm; 48 } 49 public void setComm(Double comm) { 50 this.comm = comm; 51 } 52 }
1 package dao; 2 3 import java.util.List; 4 import java.util.Set; 5 6 import vo.Emp; 7 8 public interface IEmpDAO { 9 public boolean doCreate(Emp vo) throws Exception; 10 public boolean doUpdata(Emp vo) throws Exception; 11 public boolean doRemoveBatch(Emp vo) throws Exception; 12 public boolean doRemoveBatch(Set<Integer> ids) throws Exception; 13 public Emp findById(Integer id) throws Exception; 14 public List<Emp> findAll() throws Exception; 15 public List<Emp> findAll(Integer currentPage, Integer LineSize, String column, String keyWord) throws Exception; 16 public Integer getAllCount(String column, String keyWord) throws Exception; 17 }
1 package dao.impl; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.util.List; 6 import java.util.Set; 7 8 import dao.IEmpDAO; 9 import vo.Emp; 10 11 public class EmpDAOImpl implements IEmpDAO{ 12 private Connection conn; 13 private PreparedStatement pstmt; 14 15 public EmpDAOImpl(Connection conn) { 16 this.conn = conn; 17 } 18 19 @Override 20 public boolean doCreate(Emp vo) throws Exception { 21 String sql = "INSERT INTO emp(empno,ename,job,hiredate,sal,comm) VALUES (?,?,?,?,?,?)"; 22 this.pstmt = this.conn.prepareStatement(sql); 23 this.pstmt.setInt(1, vo.getEmpno()); 24 this.pstmt.setString(2, vo.getEname()); 25 this.pstmt.setString(3, vo.getJob()); 26 this.pstmt.setDate(4, new java.sql.Date(vo.getHiredate().getTime())); 27 this.pstmt.setDouble(5, vo.getSal()); 28 this.pstmt.setDouble(6, vo.getComm()); 29 // System.out.println("insert success!"); 30 return this.pstmt.executeUpdate() > 0; 31 } 32 33 @Override 34 public boolean doUpdata(Emp vo) throws Exception { 35 return false; 36 } 37 38 @Override 39 public boolean doRemoveBatch(Emp vo) throws Exception { 40 return false; 41 } 42 43 @Override 44 public boolean doRemoveBatch(Set<Integer> ids) 45 throws Exception { 46 return false; 47 } 48 49 @Override 50 public Emp findById(Integer id) throws Exception { 51 return null; 52 } 53 54 @Override 55 public List<Emp> findAll() throws Exception { 56 return null; 57 } 58 59 @Override 60 public List<Emp> findAll(Integer currentPage, 61 Integer LineSize, String column, String keyWord) 62 throws Exception { 63 return null; 64 } 65 66 @Override 67 public Integer getAllCount(String column, 68 String keyWord) throws Exception { 69 return null; 70 } 71 72 }
1 package factory; 2 3 import java.sql.Connection; 4 import dao.IEmpDAO; 5 import dao.impl.EmpDAOImpl; 6 7 public class DAOFactory { 8 public static IEmpDAO getIEmpDAOInstance(Connection conn) { 9 return new EmpDAOImpl(conn); 10 } 11 }
1 package service; 2 import vo.Emp; 3 4 public interface IEmpService { 5 public boolean insert(Emp vo) throws Exception; 6 }
1 package service.impl; 2 3 import dbc.DatabaseConnection; 4 import factory.DAOFactory; 5 import service.IEmpService; 6 import vo.Emp; 7 8 public class EmpServiceImpl implements IEmpService{ 9 private DatabaseConnection dbc = new DatabaseConnection(); 10 11 @Override 12 public boolean insert(Emp vo) throws Exception { 13 try { 14 if(DAOFactory.getIEmpDAOInstance(this.dbc.getConnection()).findById(vo.getEmpno()) == null ) { 15 return DAOFactory.getIEmpDAOInstance(this.dbc.getConnection()).doCreate(vo); 16 } 17 System.out.println("insert false!"); 18 return false; 19 }catch(Exception e) { 20 throw e; 21 }finally { 22 this.dbc.close(); 23 } 24 } 25 }
1 package factory; 2 3 import service.IEmpService; 4 import service.impl.EmpServiceImpl; 5 6 public class ServiceFactory { 7 public static IEmpService getIEmpServiceInstance() { 8 return new EmpServiceImpl(); 9 } 10 }
1 package test; 2 3 import java.util.Date; 4 import factory.ServiceFactory; 5 import vo.Emp; 6 7 public class TestEmpInsert { 8 public static void main(String[] args) throws Exception{ 9 Emp vo = new Emp(); 10 vo.setEmpno(8888); 11 vo.setEname("Lucy"); 12 vo.setJob("Teacher"); 13 vo.setHiredate(new Date()); 14 vo.setSal(8900.0); 15 vo.setComm(5600.0); 16 System.out.println(ServiceFactory.getIEmpServiceInstance().insert(vo)); 17 } 18 }
1 package test.junit; 2 3 import java.util.Date; 4 import org.junit.Test; 5 import factory.ServiceFactory; 6 import junit.framework.TestCase; 7 import vo.Emp; 8 9 public class IEmpServiceTest { 10 11 @Test 12 public void testInsert() throws Exception { 13 Emp vo = new Emp(); 14 vo.setEmpno(8888); 15 vo.setEname("Lucy"); 16 vo.setJob("Teacher"); 17 vo.setHiredate(new Date()); 18 vo.setSal(8900.0); 19 vo.setComm(5600.0); 20 TestCase.assertTrue(ServiceFactory.getIEmpServiceInstance().insert(vo)); 21 } 22 }
参考
B/S与C/S的区别
https://zhidao.baidu.com/question/3711387.html
java中PreparedStatement和Statement详细讲解
https://blog.csdn.net/czh500/article/details/88202971
class.forName()做了什么
https://blog.csdn.net/u012292938/article/details/81033660
标签:mys dbus throw 方法 interface job code ntp sys
原文地址:https://www.cnblogs.com/cxc1357/p/12470244.html