当前位置:   article > 正文

java连接oracle数据库并进行增删减查操作

java连接oracle数据库并进行增删减查操作
  1. package com.root;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. public class Handle {
  8. // 创建一个数据库连接对象
  9. Connection connection =null;
  10. // 创建预编译语句对象
  11. PreparedStatement pstm = null;
  12. // 创建一个结果集对象
  13. ResultSet rs = null;
  14. /**
  15. * 查询数据
  16. */
  17. public void SelectData() {
  18. connection = getConnection();
  19. String sql = "select * from TB_USER ";
  20. try {
  21. pstm = connection.prepareStatement(sql);
  22. rs = pstm.executeQuery();
  23. while (rs.next()){
  24. String id = rs.getString("id");
  25. String username = rs.getString("username");
  26. String sex = rs.getString("sex");
  27. String age = rs.getString("age");
  28. System.out.println(id+"\t"+username+"\t"+sex+"\t"+age);
  29. }
  30. }catch(SQLException e) {
  31. e.printStackTrace();
  32. }finally {
  33. ReleaseResource();
  34. }
  35. }
  36. /**
  37. * 添加用户数据
  38. */
  39. public void AddData(int id,String username,int sex,String pf,int age,String school) {
  40. connection = getConnection();
  41. String sql="select count(*) from TB_USER" ;
  42. String sqlStr = "insert into TB_USER values(?,?,?,?,?,?)";
  43. try {
  44. pstm = connection.prepareStatement(sql);
  45. pstm = connection.prepareStatement(sqlStr);
  46. pstm.setInt(1, id);
  47. pstm.setString(2, username);
  48. pstm.setInt(3, sex);
  49. pstm.setString(4, pf);
  50. pstm.setInt(5, age);
  51. pstm.setString(6, school);
  52. pstm.executeQuery();
  53. }catch(SQLException e) {
  54. e.printStackTrace();
  55. }finally {
  56. ReleaseResource();
  57. }
  58. }
  59. /**
  60. * 向数据库中删除数据
  61. * @param username:根据姓名删除数据
  62. */
  63. public void DeleteData(String stuName) {
  64. connection = getConnection();
  65. String sqlStr = "delete from TB_USER where \"id\" = ?";
  66. System.out.println(stuName);
  67. try {
  68. pstm = connection.prepareStatement(sqlStr);
  69. pstm.setString(1, stuName);
  70. pstm.execute();
  71. }catch(SQLException e) {
  72. e.printStackTrace();
  73. }finally {
  74. ReleaseResource();
  75. }
  76. }
  77. /*
  78. *修改数据
  79. */
  80. public void UpdateData(String username, String sex, String pf,int age,String school,int id) {
  81. connection = getConnection();
  82. String sqlStr = "update TB_USER set \"username\"=?,\"sex\"=?,\"pf\"=?,\"age\"=?,\"school\"=? where \"id\"=?";
  83. try {
  84. // 执行插入数据操作
  85. pstm = connection.prepareStatement(sqlStr);
  86. pstm.setString(1, username);
  87. pstm.setString(2, sex);
  88. pstm.setString(3,pf);
  89. pstm.setInt(4, age);
  90. pstm.setString(5, school);
  91. pstm.setInt(6, id);
  92. pstm.executeUpdate();
  93. } catch (SQLException e) {
  94. e.printStackTrace();
  95. } finally {
  96. ReleaseResource();
  97. }
  98. }
  99. /*
  100. * 连接数据库
  101. */
  102. public static Connection getConnection() {
  103. Connection connection = null;
  104. try {
  105. System.out.println("开始尝试连接数据库");
  106. String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
  107. String user = "root";
  108. String password = "123456";
  109. connection = DriverManager.getConnection(url, user, password);
  110. System.out.println(url);
  111. System.out.println("用户名:"+user+"\t"+"密码:******");
  112. System.out.println("数据库连接成功!");
  113. return connection;
  114. }
  115. catch(Exception e){
  116. e.printStackTrace();
  117. return null;
  118. }
  119. }
  120. // 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源
  121. // 注意关闭的顺序,最后使用的最先关闭
  122. public void ReleaseResource() {
  123. if (rs != null) {
  124. try {
  125. rs.close();
  126. } catch (SQLException e) {
  127. e.printStackTrace();
  128. }
  129. }
  130. if (pstm != null) {
  131. try {
  132. pstm.close();
  133. } catch (SQLException e) {
  134. e.printStackTrace();
  135. }
  136. }
  137. if (connection != null) {
  138. try {
  139. connection.close();
  140. } catch (SQLException e) {
  141. e.printStackTrace();
  142. }
  143. }
  144. }
  145. }

然后创建一个输出类:

  1. public class Test {
  2. public static void main(String[] args) {
  3. //创建Handle对象
  4. Handle hd= new Handle();
  5. //测试增加数据操作
  6. //hd.AddData("asd","男","sdfa","dfada",16);
  7. //测试删除数据操作
  8. //hd.DeleteData("asd");
  9. //测试更新数据操作
  10. hd.UpdateData("asd","女","啊发发","dfada",18);
  11. //测试查询数据操作
  12. //hd.SelectData();
  13. }
  14. }

 

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

闽ICP备14008679号