JavaWeb应用实例servlet实现oracle基本增删改查(精) 下载本文

70. 71. }

72. out.println(\添加数据\; 73. out.println(\; 74. out.println(\; 75. conn.close(; 76. 77. }

78. catch (Exception e 79. {

80. e.printStackTrace(; 81. }

82. out.flush(; 83. out.close(; 84. } 85. 86. } 87.

UpdatePage.java //获取要修改的数据 ,并把修改过的数据提交给Update

[java] view plaincopy

1. package org.lsy.servlet; 2. import java.io.IOException; 3. import java.io.PrintWriter; 4. import java.sql.Connection; 5. import java.sql.DriverManager; 6. import java.sql.ResultSet; 7. import java.sql.Statement; 8.

9. import javax.servlet.ServletException; 10. import javax.servlet.http.HttpServlet; 11. import javax.servlet.http.HttpServletRequest;

12. import javax.servlet.http.HttpServletResponse; 13.

14. import com.sun.crypto.provider.RSACipher; 15.

16. public class UpdatePage extends HttpServlet 17. {

18. // 连接地址是由各个数据库生产商单独提供的,所以需要单独记住

19. public static final String DBURL = \LIUSY\;

20. // 连接数据库的用户名

21. public static final String DBUSER = \; 22. // 连接数据库的密码

23. public static final String DBPASS = \; 24.

25. public void doGet(HttpServletRequest request, HttpServletResponse response

26. throws ServletException, IOException 27. {

28. String id=request.getParameter(\; 29. //设置请求和响应的编码 ,不然页面上容易出现乱码 30. request.setCharacterEncoding(\; 31. response.setCharacterEncoding(\; 32. response.setContentType(\; 33. PrintWriter out = response.getWriter(; 34. try 35. {

36. Connection conn = null; // 表示数据库的连接的对象 37. Statement stmt = null; // 表示数据库的更新操作 38. // 1、使用Class类加载驱动程序

39. Class.forName(\; 40. // 2、连接数据库

41. conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS; 42. // 3、Statement接口需要通过Connection接口进行实例化操作

43. stmt = conn.createStatement(;

44. //用 rs获取查询结果 ,这里的SQL语句最好不要用* 我的字段比较少所以就偷一下懒,-,-

45. ResultSet rs=stmt.executeQuery(\+id+\;

46. while (rs.next( 47. {

48. out.println(\; 49. out.println(\; 50. out.println(\; 51. out.println(\;

52. //输入成绩的表单,提交至Update 53. out.print(\;

54. out.print(\姓名: +rs.getString( \+\

[java] view plaincopy

1. \> name='name' size=20 >\2. out.print(\班级: +rs.getString( \+ \;

3. out.print(\成绩: +rs.getString( \+ \;

4. //为了作为查询数据库的条件 ,这里要把ID传过去 5. out.print(\+id+ \; 6. out.print(\修改 '> 重置 '>\; 7. out.print(\; 8. out.println(\; 9. }

10. out.flush(; 11. out.close(; 12. }

13. catch (Exception e {

14. e.printStackTrace(;// TODO: handle exception 15. } 16. }

17. 18. }

Update.java 接收要修改的数据 ,并更新数据库

[java] view plaincopy

1. package org.lsy.servlet; 2.

3. import java.io.IOException; 4. import java.io.PrintWriter; 5. import java.sql.Connection; 6. import java.sql.DriverManager; 7. import java.sql.Statement; 8.

9. import javax.servlet.ServletException; 10. import javax.servlet.http.HttpServlet; 11. import javax.servlet.http.HttpServletRequest; 12. import javax.servlet.http.HttpServletResponse; 13.

14. public class Update extends HttpServlet 15. {

16. // 驱动程序就是之前在classpath中配置的jdbc的驱动程序的jar包中 17.

18. // 连接地址是由各个数据库生产商单独提供的,所以需要单独记住

19. public static final String DBURL = \Y\;

20. // 连接数据库的用户名

21. public static final String DBUSER = \; 22. // 连接数据库的密码

23. public static final String DBPASS = \; 24.

25. public void doGet(HttpServletRequest request, HttpServletResponse response