JAVA增删改查 代码范例

  1. import java.sql.Connection;  
  2. import java.sql.DriverManager;  
  3. import java.sql.ResultSet;  
  4. import java.sql.SQLException;  
  5. import java.sql.Statement;  
  6.   
  7.   
  8. public class DBTools {  
  9. // 定义一个方法,用来得到一个"新的"连接对象  
  10. public static Connection getConnection()  
  11. {  
  12. Connection conn = null;  
  13. String driverName = "oracle.jdbc.driver.OracleDriver";  
  14. String url = "jdbc:oracle:thin:@localhost:1521:ora9i";  
  15. String userName = "scott";  
  16. String passWord = "tiger";  
  17. try {  
  18. Class.forName(driverName);  
  19. conn = DriverManager.getConnection(url,userName,passWord );  
  20. } catch (Exception e) {  
  21. // TODO Auto-generated catch block  
  22. e.printStackTrace();  
  23. }  
  24. return conn;  
  25. }  
  26.   
  27.   
  28. public static void closeConn(Connection conn)  
  29. {  
  30. try {  
  31. if(conn != null)  
  32. {  
  33. conn.close();  
  34. }  
  35. } catch (SQLException e) {  
  36. // TODO Auto-generated catch block  
  37. e.printStackTrace();  
  38. }  
  39. }  
  40.   
  41.   
  42. public static void closeState(Statement state)  
  43. {  
  44. try {  
  45. if(state != null)  
  46. {  
  47. state.close();  
  48. }  
  49. } catch (SQLException e) {  
  50. // TODO Auto-generated catch block  
  51. e.printStackTrace();  
  52. }  
  53. }  
  54.   
  55.   
  56. public static void closeRs(ResultSet rs)  
  57. {  
  58. try {  
  59. if(rs != null)  
  60. {  
  61. rs.close();  
  62. }  
  63. } catch (SQLException e) {  
  64. // TODO Auto-generated catch block  
  65. e.printStackTrace();  
  66. }  
  67. }  
  68. }  
  69.   
  70.   
  71.   
  72.   
  73.   
  74.   
  75. import java.sql.ResultSet;  
  76. import java.sql.Statement;  
  77. import java.util.ArrayList;  
  78.   
  79.   
  80. import com.tianyitime.notebook.support.userPO.UserPO;  
  81. import com.tianyitime.notebook.support.util.DBTools;  
  82.   
  83.   
  84.   
  85.   
  86. public class UserDAO {  
  87.   
  88.   
  89. // 新增user  
  90. public void saveUserInfo(UserPO upo)  
  91. {  
  92. Connection conn = null;  
  93. Statement state = null;  
  94. try {  
  95. conn = DBTools.getConnection();  
  96. state = conn.createStatement();  
  97. String sql = "insert into notebook_user values ("+getMaxId()+",'"+upo.getYhm()+"','"+upo.getEmail()+"','"+upo.getContent()+"')";  
  98. //System.out.println(sql);  
  99. state.executeUpdate(sql);  
  100.   
  101.   
  102. } catch (Exception ex) {  
  103. // TODO Auto-generated catch block  
  104. ex.printStackTrace();  
  105. }  
  106. finally  
  107. {  
  108. DBTools.closeState(state);  
  109. DBTools.closeConn(conn);  
  110. }  
  111. }  
  112.   
  113.   
  114. //得到一个数据库中当前Id的最大值  
  115. private int getMaxId()  
  116. {  
  117. Connection conn = null;  
  118. Statement state = null;  
  119. ResultSet rs = null;  
  120. int maxId = 0;  
  121. try {  
  122. conn = DBTools.getConnection();  
  123. state = conn.createStatement();  
  124. String sql = "select max(id) maxId from notebook_user";  
  125. rs = state.executeQuery(sql);  
  126. //从resultset对象中将数据取出  
  127. if(rs.next())  
  128. {  
  129. maxId = rs.getInt("maxId");  
  130. }  
  131. } catch (Exception ex) {  
  132. // TODO Auto-generated catch block  
  133. ex.printStackTrace();  
  134. }  
  135.   
  136.   
  137. return ++maxId;  
  138. }  
  139.   
  140.   
  141. // 得到所有的记录  
  142. public ArrayList getUserInfo()  
  143. {  
  144. Connection conn = null;  
  145. Statement state = null;  
  146. ResultSet rs = null;  
  147. UserPO upo = null;  
  148. ArrayList al = new ArrayList();  
  149. try {  
  150. conn = DBTools.getConnection();  
  151. state = conn.createStatement();  
  152. String sql = "select * from notebook_user";  
  153. rs = state.executeQuery(sql);  
  154. //从resultset对象中将数据取出  
  155.   
  156.   
  157. while(rs.next())  
  158. {   
  159. upo = new UserPO();  
  160. int id = rs.getInt("id");  
  161. String yhm = rs.getString("yhm");  
  162. String email = rs.getString("email");  
  163. String content = rs.getString("content");  
  164.   
  165.   
  166. upo.setId(id);  
  167. upo.setYhm(yhm);  
  168. upo.setEmail(email);  
  169. upo.setContent(content);  
  170.   
  171.   
  172. //将改对象放入已经创建好的集合类对象ArrauyList  
  173. al.add(upo);  
  174. }  
  175. } catch (Exception ex) {  
  176. // TODO Auto-generated catch block  
  177. ex.printStackTrace();  
  178. }  
  179. finally  
  180. {  
  181. DBTools.closeRs(rs);  
  182. DBTools.closeState(state);  
  183. DBTools.closeConn(conn);  
  184. }  
  185. return al;  
  186. }  
  187.   
  188.   
  189. // 删除一条user记录  
  190. public void deleteUserInfo(int id)  
  191. {  
  192. Connection conn = null;  
  193. Statement state = null;  
  194. try {  
  195. conn = DBTools.getConnection();  
  196. state = conn.createStatement();  
  197. String sql = "delete from notebook_user where id="+id;  
  198. //System.out.println(sql);  
  199. state.executeUpdate(sql);  
  200.   
  201.   
  202. } catch (Exception ex) {  
  203. // TODO Auto-generated catch block  
  204. ex.printStackTrace();  
  205. }  
  206. finally  
  207. {  
  208. DBTools.closeState(state);  
  209. DBTools.closeConn(conn);  
  210. }  
  211. }  
  212.   
  213.   
  214. // 根据给定的信息得到记录  
  215. public ArrayList getUserInfoByInfo(String name,String email,String content)  
  216. {  
  217. Connection conn = null;  
  218. Statement state = null;  
  219. ResultSet rs = null;  
  220. UserPO upo = null;  
  221. ArrayList al = new ArrayList();  
  222. try {  
  223. conn = DBTools.getConnection();  
  224. state = conn.createStatement();  
  225. String sql = "select * from notebook_user where 1=1 ";  
  226. if(!"".equals(name) && name != null)  
  227. {  
  228. sql += " and yhm like '%"+name+"%'";  
  229. }  
  230. if(!"".equals(email) && email != null)  
  231. {  
  232. sql += " and email = '"+email+"'";  
  233. }  
  234. if(!"".equals(content) && content != null)  
  235. {  
  236. sql += " and content like '%"+content+"%'";  
  237. }  
  238. sql+=" order by id desc";  
  239. rs = state.executeQuery(sql);  
  240. //从resultset对象中将数据取出  
  241.   
  242.   
  243. while(rs.next())  
  244. {   
  245. upo = new UserPO();  
  246. int id = rs.getInt("id");  
  247. String yhm = rs.getString("yhm");  
  248. String femail = rs.getString("email");  
  249. String fcontent = rs.getString("content");  
  250.   
  251.   
  252. upo.setId(id);  
  253. upo.setYhm(yhm);  
  254. upo.setEmail(femail);  
  255. upo.setContent(fcontent);  
  256.   
  257.   
  258. //将改对象放入已经创建好的集合类对象ArrauyList  
  259. al.add(upo);  
  260. }  
  261. } catch (Exception ex) {  
  262. // TODO Auto-generated catch block  
  263. ex.printStackTrace();  
  264. }  
  265. finally  
  266. {  
  267. DBTools.closeRs(rs);  
  268. DBTools.closeState(state);  
  269. DBTools.closeConn(conn);  
  270. }  
  271. return al;  
  272. }  
  273.   
  274.   
  275. }  
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用 PHP 连接 MySQL 数据库并进行增删操作的范例代码: 连接到数据库: ```php <?php $servername = "localhost"; // 服务器名称 $username = "username"; // 用户名 $password = "password"; // 密码 $dbname = "database_name"; // 数据库名称 // 创建连接 $conn = mysqli_connect($servername, $username, $password, $dbname); // 检连接是否成功 if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } ?> ``` 插入数据: ```php <?php $sql = "INSERT INTO mytable (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')"; if (mysqli_query($conn, $sql)) { echo "Record inserted successfully"; } else { echo "Error inserting record: " . mysqli_error($conn); } ?> ``` 更新数据: ```php <?php $sql = "UPDATE mytable SET lastname='Doe Jr.' WHERE id=1"; if (mysqli_query($conn, $sql)) { echo "Record updated successfully"; } else { echo "Error updating record: " . mysqli_error($conn); } ?> ``` 删除数据: ```php <?php $sql = "DELETE FROM mytable WHERE id=1"; if (mysqli_query($conn, $sql)) { echo "Record deleted successfully"; } else { echo "Error deleting record: " . mysqli_error($conn); } ?> ``` 询数据: ```php <?php $sql = "SELECT id, firstname, lastname FROM mytable"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; } } else { echo "0 results"; } ?> ``` 注意:以上代码仅仅是一个简单的范例,实际应用中需要注意 SQL 注入问题并且使用预处理语句来避免。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值