当前位置:   article > 正文

Mydb我的数据库功能包(自己写的)没有结束时,只有进行时_mydb包

mydb包

Mydb我的数据库功能包(自己写的)没有结束时,只有进行时。

尽量写的完美,一点点增加功能,借鉴网上,借鉴老师,借鉴他人

  1. package com.xing;
  2. import java.sql.*;
  3. public class MyDb {
  4. private Connection conn;
  5. private String user;
  6. private String password;
  7. private PreparedStatement pst;
  8. private ResultSet rs;
  9. public MyDb() {
  10. try {
  11. Class.forName("com.mysql.cj.jdbc.Driver");
  12. conn = DriverManager.getConnection("jdbc:mysql://localhost/dt1?user=root&password=&serverTimezone=PRC&useSSL=false");
  13. } catch (Exception e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. public MyDb(String db, String user, String password) {
  18. this.user = user;
  19. this.password = password;
  20. try {
  21. Class.forName("com.mysql.cj.jdbc.Driver");
  22. //String url = "jdbc:mysql://localhost/dt5?serverTimezone=PRC&useSSL=false";
  23. String url = "jdbc:mysql://localhost/" + db + "?serverTimezone=PRC&useSSL=false";
  24. conn = DriverManager.getConnection(url, this.user, this.password);
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. /**
  30. * 通用型方法,可以放置任何sql语句,只是会有很多限制,需要自己注意
  31. *
  32. * @param sql
  33. */
  34. public void common(String sql) {
  35. try {
  36. Statement st = conn.createStatement();
  37. st.execute(sql);
  38. } catch (SQLException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. /**
  43. * 用于创建数据库
  44. *
  45. * @param database
  46. */
  47. public void createdatabase(String database) {
  48. String sql = "create database if not exists " + database + ";";
  49. try {
  50. Statement st = conn.createStatement();
  51. st.execute(sql);
  52. } catch (SQLException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. /**
  57. * 分页
  58. *
  59. * @param countNumber 数据数量总数
  60. * @param pageCount 每页的数量
  61. */
  62. public int page(int countNumber, int pageCount) {
  63. //得到总页数
  64. int pageMax = countNumber % pageCount == 0 ? countNumber / pageCount : (countNumber / pageCount + 1);
  65. return pageMax;
  66. }
  67. /**
  68. * 用来获取数据库中数据的总量
  69. *
  70. * @param table
  71. * @return
  72. */
  73. public int getCount(String table) {
  74. int count = 0;
  75. try {
  76. String sql = "select * from " + table;
  77. pst = conn.prepareStatement(sql);
  78. rs = pst.executeQuery();
  79. while (rs.next()) {
  80. count++;
  81. }
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. }
  85. return count;
  86. }
  87. /**
  88. * 用来查询信息
  89. * 返回的String[][]是所有的内容
  90. *
  91. * @param sql sql:查询语句
  92. * @param paras paras:想要查询的变量
  93. * paras[0]自动当成是数据库中的主键了
  94. */
  95. public String[][] selectInfo(String table, String sql, String... paras) {
  96. //得到数据库的总量
  97. int len1 = getCount(table);
  98. //得到每一个行的数据信息
  99. int len2 = paras.length;
  100. String[][] str = new String[len1][len2];
  101. System.out.println(len1);
  102. System.out.println(len2);
  103. try {
  104. //执行,向stu里面存值
  105. PreparedStatement pst = conn.prepareStatement(sql);
  106. ResultSet rs = pst.executeQuery();
  107. int add = 0;
  108. String s = "";
  109. while (rs.next()) {
  110. for (int i = 0; i < len2; i++) {
  111. s = paras[i];
  112. str[add][i] = rs.getString(s);
  113. // System.out.println(str[add][i]);
  114. }
  115. //add用来测验数组的长度设置的刚刚好
  116. add++;
  117. //System.out.println(add);
  118. }
  119. } catch (Exception e) {
  120. e.printStackTrace();
  121. }
  122. //测验所得数组是否正确
  123. /* for(int i=0;i<str.length;i++){
  124. for(int j=0;j<str[i].length;j++){
  125. System.out.print(str[i][j]+" ");
  126. }
  127. System.out.println();
  128. }*/
  129. return str;
  130. }
  131. /**
  132. * 用来查询信息
  133. * @param sql sql:查询语句
  134. * @param paras paras:想要查询的变量
  135. */
  136. /*
  137. public void selectInfo(String sql, String... paras) {
  138. try {
  139. pst = conn.prepareStatement(sql);
  140. rs = pst.executeQuery();
  141. int len = paras.length;
  142. while (rs.next()) {
  143. for (int i = 0; i < len; i++) {
  144. System.out.print(rs.getString(paras[i]) + " ");
  145. }
  146. System.out.println();
  147. }
  148. } catch (Exception e) {
  149. e.printStackTrace();
  150. }
  151. }*/
  152. /**
  153. * 用来插入信息内容
  154. *
  155. * @param sql
  156. */
  157. public void insertInfo(String sql) {
  158. try {
  159. pst = conn.prepareStatement(sql);
  160. pst.executeUpdate();
  161. } catch (Exception e) {
  162. e.printStackTrace();
  163. }
  164. }
  165. public void setConn(Connection conn) {
  166. this.conn = conn;
  167. }
  168. public void setUser(String user) {
  169. this.user = user;
  170. }
  171. public void setPassword(String password) {
  172. this.password = password;
  173. }
  174. public Connection getConn() {
  175. return conn;
  176. }
  177. public String getUser() {
  178. return user;
  179. }
  180. public String getPassword() {
  181. return password;
  182. }
  183. public PreparedStatement getPst() {
  184. return pst;
  185. }
  186. public void setPst(PreparedStatement pst) {
  187. this.pst = pst;
  188. }
  189. public ResultSet getRs() {
  190. return rs;
  191. }
  192. public void setRs(ResultSet rs) {
  193. this.rs = rs;
  194. }
  195. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/869448
推荐阅读
  

闽ICP备14008679号