您的当前位置:首页正文

jsp进行简单的增删改查

2022-03-17 来源:钮旅网
MySql5.5 + Tomcat6.0 + JSP 进行简单的增删改查

一、首先配置jdk1.6

网上有教程,简单的提一下。

进入oracle官网,找到相关的jdk,我用的是Java SE 6 1,点击JDK 下面Download

下载完成之后,可以看到是一个exe文件。

2,接下来就是安装,双击文件。

安装文件很简单,只要按照向导一路下一步就OK,但是需要主要的几个地方:

安装的路径要改一下,默认的是安装在c:/program files/下面

3,我们改成F盘,这样操作方便一点。

4, 安装完成之后就是配置环境变量

我安装的JDK默认是装在F:/JAVA目录下,结构如下

右击我的电脑 —>>

点击环境变量:

在用户变量中新建环境变量

变量名: JAVA_HOME 变量值: F:\\JAVA

变量名: classpath

变量值: F:\\JAVA\\jre\\lib\\rt.jar;.;

在系统变量中编辑path

变量值最前面加上 F:\\JAVA\\bin; (一定要加上分号)

5,测试java是否安装成功

Windows + R 打开cmd,确定

敲 java –version

若出现java version ―1.6.0_24‖则说明安装成功

至此,JDK安装完成

二、安装Tomcat服务器

1,进入Tomcat官网,进行下载

我们下载6.0的Tomcat,点击Tomcat 6.0

下载core中的第二个即可。

把刚下载好的.tar.gz文件拷贝到F盘根目录下

右击,解压到当前文件夹即可

这就是Tomcat的目录结构。

接下来打开bin文件夹中的startup.bat文件

若出现这样的情况,那就证明你安装成功了

或者更好的办法是进入http://localhost:8080/ 若显示页面那就证明成功了

至此,JDK和Tomcat都安装完成。

三、MySql安装

进入MySql官网

点击Download,接下来选择要下载的版本。

我们点下面那个Download。 下载完成之后进行安装.

双击我们已经下载好的msi文件。

根据向导进行安装。一路默认下来。但是注意:

这里要选第三个:且Character Set : utf-8

再一路下去到这里的时候

我们设置一个密码,叫root,再确认密码,然后Enable打钩.点击NEXT 直到最后Finish就OK了。这样就安装完成了

接下来就是要测试安装是否成功。

在开始菜单所有程序中找到MySql启动程序.

输入密码:root刚才我们设置的密码

若进去这个页面了,那就说明已经安装成功了

准备工作已经做完了。接下来就是要搭建环境了。

四、创建数据库

mysql>create database jsp; mysql>use jsp

mysql>create table student( ->id int(30) not null primary key, ->name varchar(50), ->age int(30), ->gender varchar(30), ->major varchar(50) ->);

至此创建了名为jsp的数据库,一个名叫student的表.

五、页面制作

在安装完成的Tomcat6.0中的webapps中新建一个文件夹normal

新建一个pic和WEB-INF文件夹,在WEB-INF下新建一个web.xml和新建一个lib文件夹

pic 文件夹中放的是图片,网页要用背景图片.自己可以到网上找找。

不要太大的。

但是名字要改成background.jpg

web.xml中的内容为:

index.jsp

addStuInfo.jsp

<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"UTF-8\"%> <%

String path = request.getContextPath(); String basePath

request.getScheme()+\"://\"+request.getServerName()+\":\"+request.getServerPort()+path+\"/\"; %>

\">

输入学生信息界面


添加学生信息


学号:

姓名:

年龄:

性别:

专业:

查询所有学生信息

delete.jsp

<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"UTF-8\"%> <%@ page import=\"java.sql.*\"%> <%

String path = request.getContextPath(); String basePath

request.getScheme()+\"://\"+request.getServerName()+\":\"+request.getServerPort()+path+\"/\"; %>

\">

删除页面

<%

request.setCharacterEncoding(\"UTF-8\"); String id = request.getParameter(\"id\"); Connection conn = null; Statement stat = null; ResultSet rs = null;

Class.forName(\"com.mysql.jdbc.Driver\"); String url = \"jdbc:mysql://localhost:3306/jsp\"; String user = \"root\";

String password = \"root\";

conn = DriverManager.getConnection(url,user,password); stat = conn.createStatement();

stat.executeUpdate(\"delete from student where id = \" + id + \"\");

=

rs = stat.executeQuery(\"select * from student\");

if(rs.next()) {

out.print(\"



删除成功!

\"); } else{

out.print(\"

删除失败!

\"); } %>

返回添加信息页面 返回信息查询页面
<%

if(rs != null) {

rs.close(); rs = null; }

if(stat != null) {

stat.close(); stat = null; }

if(conn != null) {

conn.close(); conn = null; } %>

index.jsp

<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"GB18030\"%> <%

String path = request.getContextPath(); String basePath = request.getScheme()+\"://\"+request.getServerName()+\":\"+request.getServerPort()+path+\"/\"; %>

\">

Welcome,home



点此添加学生信息

点此查询学生信息

点此修改学生信息

点此删除学生信息


insert.jsp

<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"UTF-8\"%> <%@ page import=\"java.sql.*\"%> <%

String path = request.getContextPath(); String basePath

request.getScheme()+\"://\"+request.getServerName()+\":\"+request.getServerPort()+path+\"/\"; %>

=

\">

插入学生信息

<%

request.setCharacterEncoding(\"UTF-8\"); String id = request.getParameter(\"id\");

String name = request.getParameter(\"name\"); System.out.println(name);

String age = request.getParameter(\"age\");

String gender = request.getParameter(\"gender\"); String major = request.getParameter(\"major\"); Connection conn = null; Statement stat = null; ResultSet rs = null;

Class.forName(\"com.mysql.jdbc.Driver\"); String url = \"jdbc:mysql://localhost:3306/jsp\"; String user = \"root\"; String password = \"root\";

conn = DriverManager.getConnection(url, user, password); stat = conn.createStatement();

String sql = \"insert into student(id,name,age,gender,major) values(\" + id + \age + \ stat.executeUpdate(sql);

rs = stat.executeQuery(\"select * from student\"); %>

<%

if(rs.next()) {

out.print(\"

成功输入!

\");

} else{

out.print(\"

输入失败!

\"); }

%>


返回添加信息页面 进入信息查询页面

<%

if(rs != null) {

rs.close(); rs = null; }

if(stat != null) {

stat.close(); stat = null; }

if(conn != null) {

conn.close(); conn = null; }

%>

select_for_age.jsp

<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"UTF-8\"%> <%@ page import=\"java.sql.*\"%> <%

String path = request.getContextPath(); String basePath = request.getScheme()+\"://\"+request.getServerName()+\":\"+request.getServerPort()+path+\"/\"; %>

\">

按年龄查询

<%

request.setCharacterEncoding(\"UTF-8\"); String age = request.getParameter(\"age\"); Connection conn = null; Statement stat = null; ResultSet rs = null;

Class.forName(\"com.mysql.jdbc.Driver\"); String url = \"jdbc:mysql://localhost:3306/jsp\"; String user = \"root\";

String password = \"root\";

conn = DriverManager.getConnection(url,user,password); stat = conn.createStatement();

rs = stat.executeQuery(\"select * from student where age=\" + age + \"\"); %>

符合条件的学生信息



<%

while(rs.next()) {

out.print(\"

\");

out.print(\"

\");

out.print(\"

\"); out.print(\"\");

out.print(\"

\"); out.print(\"\"); %>

<%

out.print(\"

\"); }

%>

学号 姓名 年龄 性别 专业
\" + rs.getInt(\"id\") + \"\" + rs.getString(\"name\") + \"\" + rs.getInt(\"age\") + \"\" + rs.getString(\"gender\") + \"\" + rs.getString(\"major\") + \"\">删除 \">修改


返回查询页面

<% if(rs != null) {

rs.close(); rs = null; }

if(stat != null) {

stat.close(); stat = null; }

if(conn != null) {

conn.close(); conn = null; } %>

select_for_gender

<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"UTF-8\"%> <%@ page import=\"java.sql.*\"%> <%

String path = request.getContextPath(); String basePath

request.getScheme()+\"://\"+request.getServerName()+\":\"+request.getServerPort()+path+\"/\"; %>

\">

按性别查询

<%

request.setCharacterEncoding(\"UTF-8\");

String gender = request.getParameter(\"gender\"); Connection conn = null; Statement stat = null; ResultSet rs = null;

Class.forName(\"com.mysql.jdbc.Driver\"); String url = \"jdbc:mysql://localhost:3306/jsp\"; String user = \"root\";

String password = \"root\";

conn = DriverManager.getConnection(url,user,password); stat = conn.createStatement();

rs = stat.executeQuery(\"select * from student where gender='\" + gender + \"'\"); %>

=


符合条件的学生信息



<%

while(rs.next()) {

out.print(\"

\");

out.print(\"

\");

out.print(\"

\"); out.print(\"\");

out.print(\"

\"); out.print(\"\"); %>

<%

out.print(\"

\"); } %>

学号 姓名 年龄 性别 专业
\" + rs.getInt(\"id\") + \"\" + rs.getString(\"name\") + \"\" + rs.getInt(\"age\") + \"\" + rs.getString(\"gender\") + \"\" + rs.getString(\"major\") + \"\">删除 \">修改


返回查询页面

<% if(rs != null) {

rs.close(); rs = null; }

if(stat != null) {

stat.close(); stat = null; }

if(conn != null) {

conn.close(); conn = null; } %>

select_for_id.jsp

<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"UTF-8\"%> <%@ page import=\"java.sql.*\"%> <%

String path = request.getContextPath(); String basePath

request.getScheme()+\"://\"+request.getServerName()+\":\"+request.getServerPort()+path+\"/\"; %>

\">

按学号条件查询

<%

request.setCharacterEncoding(\"UTF-8\"); String id = request.getParameter(\"id\"); Connection conn = null; Statement stat = null; ResultSet rs = null;

Class.forName(\"com.mysql.jdbc.Driver\");

=

String url = \"jdbc:mysql://localhost:3306/jsp\"; String user = \"root\";

String password = \"root\";

conn = DriverManager.getConnection(url,user,password); stat = conn.createStatement();

rs = stat.executeQuery(\"select * from student where id=\" + id + \"\"); %>

符合条件的学生信息



<%

if(rs.next()) {

out.print(\"

\");

out.print(\"

\");

out.print(\"

\"); out.print(\"\");

out.print(\"

\"); out.print(\"\"); %>

<%

out.print(\"

\"); } else{

out.print(\"

不存在此条件的信息!

\"); }

%>

学号 姓名 年龄 性别 专业
\" + rs.getInt(\"id\") + \"\" + rs.getString(\"name\") + \"\" + rs.getInt(\"age\") + \"\" + rs.getString(\"gender\") + \"\" + rs.getString(\"major\") + \"\">删除 \">修改


返回查询页面

<%

if(rs != null)

{

rs.close(); rs = null; }

if(stat != null) {

stat.close(); stat = null; }

if(conn != null) {

conn.close(); conn = null; } %>

select_for_major.jsp

<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"UTF-8\"%> <%@ page import=\"java.sql.*\"%> <%

String path = request.getContextPath(); String basePath

request.getScheme()+\"://\"+request.getServerName()+\":\"+request.getServerPort()+path+\"/\"; %>

\">

按专业查询

<%

request.setCharacterEncoding(\"UTF-8\"); // String id=request.getParameter(\"id\");

// String name=request.getParameter(\"name\"); // String age=request.getParameter(\"age\");

// String gender=request.getParameter(\"gender\"); String major=request.getParameter(\"major\"); //major=;

Connection conn = null; Statement stat = null; ResultSet rs = null;

Class.forName(\"com.mysql.jdbc.Driver\"); String url = \"jdbc:mysql://localhost:3306/jsp\"; String user = \"root\";

String password = \"root\";

conn = DriverManager.getConnection(url,user,password); stat = conn.createStatement();

rs = stat.executeQuery(\"select * from student where major='\" + major + \"'\"); %>

符合条件的学生信息



<%

while(rs.next()) {

out.print(\"

\");

out.print(\"

\");

out.print(\"

\"); out.print(\"\");

out.print(\"

\"); out.print(\"\"); %>

<%

out.print(\"

\"); } %>

学号 姓名 年龄 性别 专业
\" + rs.getInt(\"id\") + \"\" + rs.getString(\"name\") + \"\" + rs.getInt(\"age\") + \"\" + rs.getString(\"gender\") + \"\" + rs.getString(\"major\") + \" \">删除 \">修改


返回查询页面

<%

if(rs != null) {

rs.close(); rs = null; }

if(stat != null) {

stat.close(); stat = null; }

if(conn != null) {

conn.close(); conn = null; } %>

select_for_name.jsp

<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"UTF-8\"%> <%@ page import=\"java.sql.*\"%> <%

String path = request.getContextPath(); String basePath

request.getScheme()+\"://\"+request.getServerName()+\":\"+request.getServerPort()+path+\"/\";

=

%>

\">

按姓名查询

<%

request.setCharacterEncoding(\"UTF-8\"); String name = request.getParameter(\"name\"); Connection conn = null; Statement stat = null; ResultSet rs = null;

Class.forName(\"com.mysql.jdbc.Driver\"); String url=\"jdbc:mysql://localhost:3306/jsp\"; String user = \"root\";

String password = \"root\";

conn = DriverManager.getConnection(url,user,password); stat = conn.createStatement();

rs = stat.executeQuery(\"select * from student where name='\" + name + \"'\"); %>

符合条件的学生信息



<%

while(rs.next()) {

out.print(\"

\");

out.print(\"

\");

out.print(\"

\"); out.print(\"\");

out.print(\"

\"); out.print(\"\"); %>

<%

out.print(\"

\"); } %>

学号 姓名 年龄 性别 专业
\" + rs.getInt(\"id\") + \"\" + rs.getString(\"name\") + \"\" + rs.getInt(\"age\") + \"\" + rs.getString(\"gender\") + \"\" + rs.getString(\"major\") + \"\">删除 \">修改


返回查询页面

<%

if(rs != null) {

rs.close(); rs = null; }

if(stat != null) {

stat.close(); stat = null; }

if(conn != null) {

conn.close(); conn = null; } %>

showInfo.jsp

<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"UTF-8\"%> <%@ page import=\"java.sql.*\"%> <%

String path = request.getContextPath(); String basePath

request.getScheme()+\"://\"+request.getServerName()+\":\"+request.getServerPort()+path+\"/\"; %>

\">

学生信息

<%

response.setCharacterEncoding(\"UTF-8\"); request.setCharacterEncoding(\"UTF-8\"); String id = request.getParameter(\"id\");

String name = request.getParameter(\"name\"); String age = request.getParameter(\"age\");

String gender = request.getParameter(\"gender\"); String major = request.getParameter(\"major\"); Connection conn = null; Statement stat = null; ResultSet rs = null;

Class.forName(\"com.mysql.jdbc.Driver\"); String url = \"jdbc:mysql://localhost:3306/jsp\";

=

String user = \"root\";

String password = \"root\";

conn = DriverManager.getConnection(url,user,password); stat = conn.createStatement();

rs = stat.executeQuery(\"select * from student\"); %>

学生信息



全部学生信息如下

<%

while(rs.next()) {

out.print(\"

\");

out.print(\"

\");

out.print(\"

\"); out.print(\"\");

out.print(\"

\"); out.print(\"\"); %>

<%

out.print(\"

\"); }

%>

学号 姓名 年龄 性别 专业
\" + rs.getInt(\"id\") + \"\" + rs.getString(\"name\") + \"\" + rs.getInt(\"age\") + \"\" + rs.getString(\"gender\") + \"\" + rs.getString(\"major\") + \"\">删除 \">修改

按学号查询:


按姓名查询:


按年龄查询:


按性别查询:


按专业查询:



返回添加学生信息页面


<%

if(rs != null) {

rs.close(); rs = null; }

if(stat != null) {

stat.close(); stat = null; }

if(conn != null)

{

conn.close(); conn = null; } %>

update.jsp

<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"UTF-8\"%> <%@ page import=\"java.sql.*\"%> <%

String path = request.getContextPath(); String basePath

request.getScheme()+\"://\"+request.getServerName()+\":\"+request.getServerPort()+path+\"/\"; %>

\">

学生信息

<%

response.setCharacterEncoding(\"UTF-8\"); request.setCharacterEncoding(\"UTF-8\"); String id = request.getParameter(\"id\"); Connection conn = null; Statement stat = null; ResultSet rs = null;

Class.forName(\"com.mysql.jdbc.Driver\"); String url = \"jdbc:mysql://localhost:3306/jsp\";

String user = \"root\";

String password = \"root\";

conn = DriverManager.getConnection(url,user,password); stat = conn.createStatement();

rs = stat.executeQuery(\"select * from student where id=\" + id + \"\"); %>

学生信息



要修改的学生信息如下

<%

while(rs.next()) {

out.print(\"

\");

out.print(\"

\");

out.print(\"

\"); out.print(\"\");

out.print(\"

\"); out.print(\"\"); out.print(\"\");

%>

学号 姓名 年龄 性别 专业
\" + rs.getInt(\"id\") + \"\" + rs.getString(\"name\") + \"\" + rs.getInt(\"age\") + \"\" + rs.getString(\"gender\") + \"\" + rs.getString(\"major\") + \"


将学生信息更改为:

学号:\" title=\"学号不能改变\" readonly=\"readonly\">

姓名:

年龄:

性别:

专业:


返回添加信息页面

返回信息查询页面

<% } %>

<%

if(rs != null) {

rs.close(); rs = null; }

if(stat != null) {

stat.close(); stat = null; }

if(conn != null) {

conn.close(); conn = null; } %>

updateShow.jsp

<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"UTF-8\"%> <%@ page import=\"java.sql.*\"%> <%

String path = request.getContextPath(); String basePath

request.getScheme()+\"://\"+request.getServerName()+\":\"+request.getServerPort()+path+\"/\"; %>

=

\">

学生信息

<%

response.setCharacterEncoding(\"UTF-8\"); request.setCharacterEncoding(\"UTF-8\"); String id = request.getParameter(\"id\"); Connection conn = null; Statement stat = null; ResultSet rs = null;

Class.forName(\"com.mysql.jdbc.Driver\"); String url = \"jdbc:mysql://localhost:3306/jsp\"; String user = \"root\";

String password = \"root\";

conn = DriverManager.getConnection(url,user,password); stat = conn.createStatement();

rs = stat.executeQuery(\"select * from student where id=\" + id + \"\"); %>

学生信息



要修改的学生信息如下

<%

while(rs.next()) {

out.print(\"

\");

out.print(\"

\");

out.print(\"

\"); out.print(\"\");

out.print(\"

\"); out.print(\"\"); out.print(\"\");

%>

学号 姓名 年龄 性别 专业
\" + rs.getInt(\"id\") + \"\" + rs.getString(\"name\") + \"\" + rs.getInt(\"age\") + \"\" + rs.getString(\"gender\") + \"\" + rs.getString(\"major\") + \"


将学生信息更改为:

学号:\" title=\"学号不能改变\" readonly=\"readonly\">

姓名:

年龄:

性别:

专业:


返回添加信息页面

返回信息查询页面

<% } %>

<%

if(rs != null) {

rs.close(); rs = null; }

if(stat != null) {

stat.close(); stat = null; }

if(conn != null)

{

conn.close(); conn = null; } %>

整个结构如下:

接下就进行最后一步了。

我们要下载一个驱动mysql-conn-java-5.1.20的zip文件包

下载第二个、保存。

解压之后找到类似于这样的jar文件

复制到normal下面的WEB-INF下的lib文件夹中,一切搞定

打开tomcat文件夹的bin文件夹

找到 startup.bat 双击打开

打开浏览器

在地址栏输入http://localhost:808/normal/

结果界面如上,然后自己去熟悉一下吧.

因篇幅问题不能全部显示,请点此查看更多更全内容