JSP脚本连接数据库
2024-08-31 12:23:49
入门
简单的jsp文件
<%--
Created by IntelliJ IDEA.
User: e550
Date: 2017/1/9
Time: 23:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" import="java.util.*" language="java" %>
<html>
<head>
<title>欢迎</title>
</head>
<body>
欢迎学习Java web知识!
现在的时间是:
<%out.println(new java.util.Date());%>
</body>
</html>
声明
<%--
Created by IntelliJ IDEA.
User: e550
Date: 2017/1/9
Time: 23:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" import="java.util.*" language="java" %>
<html>
<head>
<title>声明示例</title>
</head>
<%!
// 声明一个整型变量
public int count;
// 声明一个方法
public String info() {
return "hello java";
}
%>
<body>
<%
// 输出变量
out.println(++count); // 加1后输出1
// 输出方法的返回值
out.println(info());
%>
</body>
</html>
变量输出
<%--
Created by IntelliJ IDEA.
User: e550
Date: 2017/1/9
Time: 23:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" import="java.util.*" language="java" %>
<html>
<head>
<title>输出表达式</title>
</head>
<%!
// 声明一个整型变量
public int count;
// 声明一个方法
public String info() {
return "hello java";
}
%>
<body>
<%=
count++
%>
<%=
info()
%>
</body>
</html>
小脚本测试
<%--
Created by IntelliJ IDEA.
User: e550
Date: 2017/1/9
Time: 23:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" import="java.util.*" language="java" %>
<html>
<head>
<title>小脚本测试</title>
</head>
<body>
<table bgcolor="#bdb76b" border="1" width="300px">
<%
for(int i=0;i<10;i++) {
%>
<tr>
<td>循环值:</td>
<td><%=i%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
连接数据库
<%--
Created by IntelliJ IDEA.
User: e550
Date: 2017/1/9
Time: 23:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>小脚本测试</title>
</head>
<body>
<%
// 注册数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_mysql","root","123456");
// 创建Statement
Statement stmt = conn.createStatement();
// 执行查询
ResultSet rs = stmt.executeQuery("select * from tp_goods_type;");
%>
<table bgcolor="#bdb76b" border="1px" width="300px">
<%
// 遍历结果集
while (rs.next())
{
%>
<tr>
<td>
<%= rs.getString(1)%>
</td>
<td>
<%= rs.getString(2)%>
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
这里注意了,需要在编辑器中配置,
否则无法连接数据库!
最新文章
- IOS 封装功能和逻辑思想
- Web API - Video File Streaming
- 使用 WordPress 插件模板开发高质量插件
- Redis教程(十):持久化详解
- 百度地图API 学习网站
- Error -26359: Function not allowed within a concurrent group
- tomcat配置多个web网站的配置详解
- 在C#中使用WIA获取扫描仪数据
- 修改vim中的tab为4个空格
- android一分钟学会可视化操作数据库(无需ROOT)
- Rest之路 -- 从第二个Rest application里面分析 Rest 方法
- redis 主从配置实例、注意事项、及备份方式
- .Net代码控制PrivateBinPath和ConfigurationFile的位置
- cookie——创建、获取、删除
- P图软件瘦脸眼睛放大和手动瘦身功能
- Java学习——Applet菜单
- 基于Docker持续交付平台建设的实践
- Solidworks直接打开SWB文件报错怎么办
- sgu 129 Inheritance 凸包,线段交点,计算几何 难度:2
- 1.1.4 A+B for Input-Output Practice (V)