JdbcCodeDao.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. import java.util.Properties;
  10. public class JdbcCodeDao implements CodeDao {
  11. private static String driverClassName; //启动驱动
  12. private static String url; //设置连接路径
  13. private static String username; //数据库用户名
  14. private static String password; //数据库连接密码
  15. static{
  16. readJdbcProperties();
  17. }
  18. public static void readJdbcProperties() {
  19. Properties props = new Properties();
  20. // 第一种,通过类加载器进行获取properties文件流-->
  21. // InputStream in = JdbcCodeDao.class.getClassLoader().getResourceAsStream("db.properties");
  22. // 第二种,通过类进行获取properties文件流-->
  23. InputStream in = JdbcCodeDao.class.getResourceAsStream("/jdbc.properties");
  24. try {
  25. props.load(in);
  26. driverClassName = props.getProperty("driver");
  27. url = props.getProperty("url");
  28. username = props.getProperty("username");
  29. password = props.getProperty("password");
  30. } catch (IOException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. }
  35. @Override
  36. public void add(Code form) {
  37. Connection con = null; //连接
  38. PreparedStatement pstmt = null; //使用预编译语句
  39. ResultSet rs = null; //获取的结果集
  40. try {
  41. Class.forName(driverClassName); //执行驱动
  42. con = DriverManager.getConnection(url, username, password); //获取连接
  43. String sql = "INSERT INTO CODE VALUES(?,?)"; //设置的预编译语句格式
  44. pstmt = con.prepareStatement(sql);
  45. pstmt.setString(1, form.getName());
  46. pstmt.setString(2, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
  47. pstmt.executeUpdate();
  48. } catch (Exception e) {
  49. throw new RuntimeException(e);
  50. }finally {
  51. //关闭资源,倒关
  52. try {
  53. if(rs != null) rs.close();
  54. if(pstmt != null) pstmt.close();
  55. if(con != null) con.close(); //必须要关
  56. } catch (Exception e) {
  57. }
  58. }
  59. }
  60. @Override
  61. public Code findByName(String name) {
  62. // String driverClassName = "com.mysql.jdbc.Driver";
  63. // String url = "jdbc:mysql://118.190.47.230:3306/ys_equipment_cs"; //设置连接路径
  64. // String username = "root"; //数据库用户名
  65. // String password = "p011430seya1026"; //数据库连接密码
  66. Connection con = null;
  67. PreparedStatement pstmt = null;
  68. ResultSet rs = null;
  69. try {
  70. Class.forName(driverClassName);
  71. con = DriverManager.getConnection(url, username, password);
  72. String sql = "SELECT * FROM CODE WHERE name=?";
  73. pstmt = con.prepareStatement(sql);
  74. pstmt.setNString(1, name);
  75. rs = pstmt.executeQuery();
  76. if(rs == null) {
  77. return null;
  78. }
  79. if(rs.next()) {
  80. Code code = new Code();
  81. code.setName(rs.getString("name"));
  82. code.setIndate(rs.getString("indate"));
  83. return code;
  84. } else {
  85. return null;
  86. }
  87. } catch (Exception e) {
  88. throw new RuntimeException(e);
  89. }finally {
  90. //关闭资源,倒关
  91. try {
  92. if(rs != null) rs.close();
  93. if(pstmt != null) pstmt.close();
  94. if(con != null) con.close(); //必须要关
  95. } catch (Exception e) {
  96. }
  97. }
  98. }
  99. }