当前位置:Gxlcms > 数据库问题 > 练习JavaWeb连接数据库

练习JavaWeb连接数据库

时间:2021-07-01 10:21:17 帮助过:11人阅读

="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="Check.jsp" method="post"> 卡号:<input type="text" name="cardid"><br> 密码:<input type="password" name="password"><br> 按钮:<input type="submit" value="登录"> </form> </body> </html>

3.创建接收页面

  1. <%@page <span style="color: #0000ff">import</span>="java.sql.ResultSet"%>
  2. <%@page <span style="color: #0000ff">import</span>="java.sql.PreparedStatement"%>
  3. <%@page <span style="color: #0000ff">import</span>="java.sql.Connection"%>
  4. <%@page <span style="color: #0000ff">import</span>="java.sql.DriverManager"%>
  5. <%@ page language="java" contentType="text/html; charset=UTF-8"<span style="color: #000000">
  6. pageEncoding</span>="UTF-8"%>
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  8. <html>
  9. <head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  11. <title>Insert title here</title>
  12. </head>
  13. <body>
  14. <%
  15. <span style="color: #008000">//</span><span style="color: #008000">接收验证</span>
  16. String cardid = request.getParameter("cardid"<span style="color: #000000">);
  17. String password </span>= request.getParameter("password"<span style="color: #000000">);
  18. </span><span style="color: #008000">//</span><span style="color: #008000">数据验证</span>
  19. <span style="color: #0000ff">if</span>(cardid.equals("")||password.equals(""<span style="color: #000000">))
  20. {
  21. out.write(</span>"请正确登录系统"<span style="color: #000000">);
  22. }
  23. </span><span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span>(cardid==<span style="color: #0000ff">null</span>||password==<span style="color: #0000ff">null</span><span style="color: #000000">)
  24. {
  25. out.write(</span>"请正确登录系统"<span style="color: #000000">);
  26. }
  27. </span><span style="color: #0000ff">else</span><span style="color: #000000">
  28. {
  29. out.write(cardid);
  30. out.write(password);
  31. </span><span style="color: #008000">//</span><span style="color: #008000">连接数据库</span>
  32. Class.forName("oracle.jdbc.driver.OracleDriver"<span style="color: #000000">);
  33. Connection conn </span>=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "test0816", "laoer123"<span style="color: #000000">);
  34. PreparedStatement ps </span>= conn.prepareStatement("select * from t_balance where card_id=? and password=? "<span style="color: #000000">);
  35. ps.setString(</span>1<span style="color: #000000">, cardid);
  36. ps.setString(</span>2<span style="color: #000000">,password);
  37. ResultSet rs </span>=<span style="color: #000000">ps.executeQuery();
  38. </span><span style="color: #008000">//</span><span style="color: #008000">数据验证</span>
  39. <span style="color: #0000ff">if</span><span style="color: #000000">(rs.next())
  40. {
  41. out.write(</span>"用户名和密码正确"<span style="color: #000000">);
  42. }
  43. </span><span style="color: #0000ff">else</span><span style="color: #000000">
  44. {
  45. out.write(</span>"用户名或密码错误"<span style="color: #000000">);
  46. }
  47. rs.close();
  48. ps.close();
  49. conn.close();
  50. }
  51. </span>%>
  52. </body>
  53. </html>

4.通过连接池连接数据库

 (1)添加jar包,同上。

 (2)在JavaResources - src- 创建java包 -创建类 ,并将配置文件放在src下

    

  1. <span style="color: #0000ff">package</span><span style="color: #000000"> com.hanqi.web;
  2. </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.sql.Connection;
  3. </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.sql.PreparedStatement;
  4. </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.sql.ResultSet;
  5. </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.sql.SQLException;
  6. </span><span style="color: #0000ff">import</span><span style="color: #000000"> com.mchange.v2.c3p0.ComboPooledDataSource;
  7. </span><span style="color: #008000">//</span><span style="color: #008000">单例模式</span>
  8. <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> CardDAO {
  9. </span><span style="color: #008000">//</span><span style="color: #008000">构建连接池对象并配置</span>
  10. <span style="color: #0000ff">private</span> ComboPooledDataSource cpds =<span style="color: #0000ff">new</span> ComboPooledDataSource("helloc3p0"<span style="color: #000000">);
  11. </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span><span style="color: #000000"> checklogin(String cardid,String password)
  12. {
  13. </span><span style="color: #0000ff">boolean</span> rtn =<span style="color: #0000ff">false</span><span style="color: #000000">;
  14. </span><span style="color: #0000ff">try</span><span style="color: #000000"> {
  15. Connection conn </span>=<span style="color: #000000"> cpds.getConnection();
  16. PreparedStatement ps </span>= conn.prepareStatement("select * from t_balance where card_id=? and password=? "<span style="color: #000000">);
  17. ps.setString(</span>1<span style="color: #000000">, cardid);
  18. ps.setString(</span>2<span style="color: #000000">,password);
  19. ResultSet rs </span>=<span style="color: #000000">ps.executeQuery();
  20. rtn</span>=<span style="color: #000000">rs.next();
  21. rs.close();
  22. ps.close();
  23. conn.close();
  24. } </span><span style="color: #0000ff">catch</span><span style="color: #000000"> (SQLException e) {
  25. </span><span style="color: #008000">//</span><span style="color: #008000"> TODO Auto-generated catch block</span>
  26. <span style="color: #000000"> e.printStackTrace();
  27. }
  28. </span><span style="color: #0000ff">return</span><span style="color: #000000"> rtn;
  29. }
  30. }
  31. </span>

请求信息页面

  1. <span style="background-color: #ffff00; color: #000000"><%</span><span style="background-color: #f5f5f5; color: #000000">@ page language</span><span style="background-color: #f5f5f5; color: #000000">=</span><span style="background-color: #f5f5f5; color: #800000">"</span><span style="background-color: #f5f5f5; color: #800000">java</span><span style="background-color: #f5f5f5; color: #800000">"</span><span style="background-color: #f5f5f5; color: #000000"> contentType</span><span style="background-color: #f5f5f5; color: #000000">=</span><span style="background-color: #f5f5f5; color: #800000">"</span><span style="background-color: #f5f5f5; color: #800000">text/html; charset=UTF-8</span><span style="background-color: #f5f5f5; color: #800000">"</span><span style="background-color: #f5f5f5; color: #000000">
  2. pageEncoding</span><span style="background-color: #f5f5f5; color: #000000">=</span><span style="background-color: #f5f5f5; color: #800000">"</span><span style="background-color: #f5f5f5; color: #800000">UTF-8</span><span style="background-color: #f5f5f5; color: #800000">"</span><span style="background-color: #ffff00; color: #000000">%></span>
  3. <span style="color: #0000ff"><!</span><span style="color: #ff00ff">DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"</span><span style="color: #0000ff">></span>
  4. <span style="color: #0000ff"><</span><span style="color: #800000">html</span><span style="color: #0000ff">></span>
  5. <span style="color: #0000ff"><</span><span style="color: #800000">head</span><span style="color: #0000ff">></span>
  6. <span style="color: #0000ff"><</span><span style="color: #800000">meta </span><span style="color: #ff0000">http-equiv</span><span style="color: #0000ff">="Content-Type"</span><span style="color: #ff0000"> content</span><span style="color: #0000ff">="text/html; charset=UTF-8"</span><span style="color: #0000ff">></span>
  7. <span style="color: #0000ff"><</span><span style="color: #800000">title</span><span style="color: #0000ff">></span>Insert title here<span style="color: #0000ff"></</span><span style="color: #800000">title</span><span style="color: #0000ff">></span>
  8. <span style="color: #0000ff"></</span><span style="color: #800000">head</span><span style="color: #0000ff">></span>
  9. <span style="color: #0000ff"><</span><span style="color: #800000">body</span><span style="color: #0000ff">></span>
  10. <span style="color: #0000ff"><</span><span style="color: #800000">form </span><span style="color: #ff0000">action</span><span style="color: #0000ff">="Check2.jsp"</span><span style="color: #ff0000"> method</span><span style="color: #0000ff">="post"</span><span style="color: #0000ff">></span><span style="color: #000000">
  11. 卡号:</span><span style="color: #0000ff"><</span><span style="color: #800000">input </span><span style="color: #ff0000">type</span><span style="color: #0000ff">="text"</span><span style="color: #ff0000"> name</span><span style="color: #0000ff">="cardid"</span><span style="color: #0000ff">><</span><span style="color: #800000">br</span><span style="color: #0000ff">></span><span style="color: #000000">
  12. 密码:</span><span style="color: #0000ff"><</span><span style="color: #800000">input </span><span style="color: #ff0000">type</span><span style="color: #0000ff">="password"</span><span style="color: #ff0000"> name</span><span style="color: #0000ff">="password"</span><span style="color: #0000ff">><</span><span style="color: #800000">br</span><span style="color: #0000ff">></span><span style="color: #000000">
  13. 按钮:</span><span style="color: #0000ff"><</span><span style="color: #800000">input </span><span style="color: #ff0000">type</span><span style="color: #0000ff">="submit"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="登录"</span><span style="color: #0000ff">></span>
  14. <span style="color: #0000ff"></</span><span style="color: #800000">form</span><span style="color: #0000ff">></span>
  15. <span style="color: #0000ff"></</span><span style="color: #800000">body</span><span style="color: #0000ff">></span>
  16. <span style="color: #0000ff"></</span><span style="color: #800000">html</span><span style="color: #0000ff">></span>

接收信息的页面

  1. <%@page <span style="color: #0000ff">import</span>="com.hanqi.web.CardDAO"%>
  2. <%@ page language="java" contentType="text/html; charset=UTF-8"<span style="color: #000000">
  3. pageEncoding</span>="UTF-8"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. <%
  12. <span style="color: #008000">//</span><span style="color: #008000"> 设置不缓存页面</span>
  13. response.setHeader("Cache-Control", "no-cache"<span style="color: #000000">);
  14. </span><span style="color: #008000">//</span><span style="color: #008000">定时跳转
  15. </span><span style="color: #008000">//</span><span style="color: #008000">response.setHeader("refresh", "2;URL=</span><span style="color: #008000; text-decoration: underline">http://www.baidu.com</span><span style="color: #008000">");
  16. </span><span style="color: #008000">//
  17. </span><span style="color: #000000">
  18. String cardid </span>= request.getParameter("cardid"<span style="color: #000000">);
  19. String password </span>=request.getParameter("password"<span style="color: #000000">);
  20. </span><span style="color: #0000ff">if</span>(cardid==<span style="color: #0000ff">null</span>||password==<span style="color: #0000ff">null</span>||<span style="color: #000000">
  21. cardid.equals(</span>"")||password.equals(""<span style="color: #000000">))
  22. {
  23. out.write(</span>"请正确登录"<span style="color: #000000">);
  24. }
  25. </span><span style="color: #0000ff">else</span><span style="color: #000000">
  26. {
  27. </span><span style="color: #008000">//</span><span style="color: #008000">检查登录信息</span>
  28. CardDAO cd = <span style="color: #0000ff">new</span><span style="color: #000000"> CardDAO();
  29. </span><span style="color: #0000ff">if</span><span style="color: #000000">(cd.checklogin(cardid, password))
  30. {
  31. </span><span style="color: #008000">//</span><span style="color: #008000">out.write("登录成功!");</span>
  32. <span style="color: #000000">
  33. response.getWriter().write(</span>"验证成功!"<span style="color: #000000">);
  34. </span><span style="color: #008000">//</span><span style="color: #008000">页面跳转
  35. </span><span style="color: #008000">//</span><span style="color: #008000">response.sendRedirect("Main.jsp");</span>
  36. response.sendRedirect("http://www.baidu.com"<span style="color: #000000">);
  37. }
  38. </span><span style="color: #0000ff">else</span><span style="color: #000000">
  39. {
  40. out.write(</span>"登录失败!"<span style="color: #000000">);
  41. </span><span style="color: #008000">//</span><span style="color: #008000">跳回登录页面</span>
  42. response.setHeader("refresh", "2;URL=loginin.jsp"<span style="color: #000000">);
  43. }
  44. }
  45. </span>%>
  46. </body>
  47. </html>

附原本的图,不是以上程序

技术分享

技术分享

技术分享

技术分享

 

练习JavaWeb连接数据库

标签:print   ora   string   acl   private   程序   test   web   line   

人气教程排行