用java做网站,java连接数据库并查询输出到页面

  1. 云栖社区>
  2. 博客>
  3. 正文

用java做网站,java连接数据库并查询输出到页面

幸运券发放 2018-06-23 10:38:02 浏览826
展开阅读全文

java web的后缀名是jsp,所以咱们要有一个jsp的开发环境,我这用的是jspStudy


自行百度。这软件是一个集成开发环境,安装启动后即可使用,集成了tomcat和mysql数据库

首先我们先新建一个首页文件

index.jsp

[java] view plain copy
  1. <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>  
  2. <%@ page import="java.sql.Connection" %>  
  3. <%@ page import="java.sql.DriverManager" %>  
  4. <%@ page import="java.sql.SQLException" %>  
  5. <%@ page import="java.sql.Statement" %>  
  6. <%@ page import="java.sql.PreparedStatement" %>  
  7. <%@ page import="java.sql.ResultSet" %>  
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  9. <html xmlns="http://www.w3.org/1999/xhtml">  
  10. <head>  
  11. <meta http-equiv="content-type" content="text/html; charset=utf-8" />  
  12. <title>mysql测试</title>  
  13.   
  14. </head>  
  15. <body>  
  16.   
  17.     <%  
  18.              //此进行连接数据库  
  19.             String url="jdbc:mysql://127.0.0.1:3306/test"; //test为数据库名称  
  20.             String dbuser="root"//数据库账户  
  21.             String dbpwd="root"//数据库密码  
  22.             try   
  23.             {  
  24.                 Class.forName("com.mysql.jdbc.Driver"); //加载驱动 JspStudy  
  25.             }   
  26.             catch (ClassNotFoundException e)   
  27.             {   
  28.                 e.printStackTrace();  
  29.             }    
  30.             //取得数据库连接conn  
  31.             Connection conn=DriverManager.getConnection(url, dbuser, dbpwd);;  
  32.   
  33.             PreparedStatement ps=null;  
  34.             ResultSet rs=null;  
  35.             //声明数据库字段  
  36.             String id="";  
  37.             String title="";  
  38.             String img="";  
  39.             try   
  40.             {   
  41.                 String sql="select * from res";  
  42.                 ps = conn.prepareStatement(sql);  
  43.                 rs = ps.executeQuery();  
  44.                 while(rs.next())  
  45.                 {  
  46.                     id=rs.getString(1);  
  47.                     title=rs.getString(2);  
  48.                     img=rs.getString(3);  
  49.                     out.println("ID:"+id+"<br>");  
  50.                     out.println(title+"<br><br>");   
  51.                     out.println("<img src=\""+img+"\"/>"+"<br><br>");   
  52.                 }  
  53.             }  
  54.             catch (SQLException e)   
  55.             {  
  56.                 e.printStackTrace();  
  57.             }  
  58.             finally  
  59.             {   
  60.                     try   
  61.                     {  
  62.                         if(rs!=null)              
  63.                             rs.close();  
  64.                     }   
  65.                     catch (SQLException e)   
  66.                     {  
  67.                         e.printStackTrace();  
  68.                     }  
  69.                     finally  
  70.                     {  
  71.                             try   
  72.                             {  
  73.                                 if(ps!=null)                      
  74.                                     ps.close();  
  75.                             }   
  76.                             catch (SQLException e)   
  77.                             {   
  78.                                 e.printStackTrace();  
  79.                             }  
  80.                             finally  
  81.                             {  
  82.                                 try   
  83.                                 {  
  84.                                     if(conn!=null)                    
  85.                                         conn.close();  
  86.                                 }   
  87.                                 catch (SQLException e)   
  88.                                 {   
  89.                                         e.printStackTrace();  
  90.                                 }  
  91.                             }  
  92.                     }  
  93.             }   
  94.     %>         
  95. </body>  
  96. </html>  

然后拷贝到jspStudy的WWW目录
在浏览器输入http://localhost/index.jsp
即可运行。

当然数据库的数据要有

这是我这边数据库的结构

原文地址https://blog.csdn.net/weixin_39927850/article/details/80773736

网友评论

作者关闭了评论
幸运券发放
+ 关注