配置Web应用环境实现JSP留言簿_MySQL
Java Server Page(简称JSP),和ASP、PHP一样都是网络编程语言,只不过在JSP页面中插入的脚本代码是Java语句片段。要利用JSP编写应用,首先,必须要有一个能执行JSP脚本的Web服务器,可以在原有的Apache、IIS或PWS服务器的基础上建立,不过有许多技术上的问题。建议刚接触JSP的虫们,还是白手起家,直接安装一个专门支持JSP的Web服务器,以免节外生枝,这里给大家介绍的是Tomcat 3.1。
熟悉网络编程的人都知道,在网络编程中要是没有数据库的支持,一件很简单的事做起来都是相当的辛苦。那么在Java环境下使用什么数据库比较好呢?目前,流行的网络数据库主要有Oracle、Sybase、SQL Server、MySQL等,不过最适合个人网站或小型网络使用的首推MySQL,其完全免费、容易安装、容易管理、容易获得、完全支持SQL语言等特点,为目前广大网络编程爱好者所喜欢。本文介绍一下在Windows环境下如何安装Java SDK、Tomcat、MySQL、JDBC for MySQL。
软件准备
首先要下载到下面这些程序:
1、java编译支持环境:j2sdk1_3_-win.exe(30MB)
2、JDBC for MySQL驱动程序:mm.mysql.jdbc-1.2b.zip(386KB)
3、支持JSP的Web服务器Tomcat3.1:jakarta-to,cat.zip(2.23MB)
4、MySQL数据库存服务器:mysql-3.23.21-beta-win-src.zip(2.23MB)
以上程序除MySQL数据库存服务器外,在http://java.sun.com/中都有最新版的程序,在各大下载中心也都很容易下载到。
安装具有JDBC for MySQL的JSP开发环境
安装Java 开发工具包
在用户访问Tomcat服务器的JSP页面时,Tomcat首先调用Java开发工具包,编译并执行JSP页面中的Java代码,将结果以HTML的格式返回给客户。因此在安装Tomcat之前,必须先安装Java开发工具包。安装时直接双击下载的j2sdk1_3_0-win.exe,就可以安装Java开发环境,惟一需要操作的就是选择一下安装目录,这里假设安装在C:\JDK1.3目录下。
另外, 如果选用中文的Windows,必须修改系统注册表,因为在安装过程中,系统用中文注册“Java 运行时环境”这一分支,该分支用于指明Java运行时所需文件的目录,而Java SDK不能识别系统注册表中的中文数据。
具体步骤是:用RegEdit打开注册表并找到Javasoft项, 位置为:hkey_local_machine→software→javasoft,找到 “Java 运行时环境” ,把这个分支导出到文件1.reg中。然后用文本编辑器打开1.reg,把其中的所有“Java 运行时环境”替换成“Java Runtime Environment”并保存,双击该文件导入注册表。
接着是设置环境变量。在Win 9x中,要编辑Autoexec.bat文件,用Set 语句来设定环境变量。在Win NT或Win 2000中可以选择“我的电脑”,右键点出选单,选择“属性”,弹出“系统特性”对话框, 选择“高级”,然后点按钮“环境变量”,就可以编辑系统的环境变量了。
在其中加入如下语句:
rem 设置路径
PATH=%PATH%;c:\jdk1.3;c:\jdk1.3\bin
rem 设置java环境变量
set CLASSPATH=C:\jdk1.3\lib\Tools.jar;C:\jdk1.3\lib\dt.jar;
rem 设置java主目录
set JAVA_HOME=c:\jdk1.3
安装Tomcat 3.1
安装Tomcat 3.1比较简单,直接把jakarta-tomcat.zip解压释放到C:\Tomcat目录下即可,接着是设置环境变量,加入如下语句:
rem 设置路径
PATH=%PATH%;c:\tomcat
rem 设置tomcat环境变量
set CLASSPATH=c:\tomcat\classes;c:\tomcat\lib;%CLASSPATH
rem 设置TOMcat的主目录
set TOMCAT_HOME=c:\tomcat
重新启动电脑后,你就拥有了一台支持JSP的Web服务器。运行C:\Tomcat\Bin目录下Startup.bat后出现两个命令行方式的窗口。这时在浏览器中键入 http://localhost:8080/,应该可以看到Tomcat 3.1的画面了。对了,千万不要把这两个窗口关了,它表示是Java和Tomcat正在后台运行。要关闭服务器可执行C:\Tomcat\Bin目录下Shutdown.bat。
安装MySQL数据库管理系统
将下载的mysql-3.23.21-beta-win-src.zip解压缩到一个临时目录,运行临时目录中的Setup,根据安装向导选择好安装目录和安装方式,系统就会完成安装。这里假设安装在C:\MySQL目录下。运行C:\MySQL\Bin\目录下的mysqld-shareware.exe就可以启动MySQL了。启动MySQL后,Windows桌面没什么变化,可以通过执行MySQL Manager来检查数据库服务是否安装成功。如果安装成功,你就可以直接打开里面的库Test和MySQL。
安装JDBC for MySQL
为了使Java能操作MySQL的数据库,需要安装MySQL的JDBC驱动程序,将mm.mysql.jdbc-1.2b.zip解压到C:\下,自动生成一个mm.mysql.jdbc-1.2b的目录,并设置环境变量:
rem 设置mysql.jdbc环境变量
set CLASSPATH=c:\mm.mysql.jdbc-1.2b;%CLASSPATH
重新启动后,所有的安装就全部OK了!
例子:使用环境建立留言簿
建立存放数据的表
要设计一个留言簿,首先必须在MySQL的数据库中建立一个存放留言数据的表,假设该表为Questbook,其结构如下:
Recordid 存放记录号
name 存放留言者的姓名
Email 存放留言者的邮件地址
body 存放留言者的留言
具体操作步骤:
1.执行程序mysqld-shareware.exe,启动MySQL。
2.执行程序MySQLManager,打开库Test。
3.选择Tools选单下的SQL Query ,出现MySQL Query窗口。
4.在Query标签下,输入命令如下:create table questbook(Recordid int,Name char(20),Email char(30), Body text)。
5.单击运行图标。你就可以得到用于存放留言簿数据的表Questbook。
编写留言簿程序
Java是通过JDBC for MySQL提供的工具包中的API函数,来调用MySQL的数据库,你可以用浏览器打开C:\mm.mysql.jdbc-1.2b\doc\apidoc\index.html文件来获得所有API函数说明。下面我只向大家介绍几个编写留言簿所需的函数。
Class.forName("org.gjt.mm.mysql.Driver"); 用于加载 mm.mysql驱动程序
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root;password="); 用于连接本地数据库MySQL
stmt = con.createStatement(); 生成数据库对象
rs = stmt.executeQuery("SELECT * FROM guestbook");执行SQL语句并返回结果集
将程序编好后放在Tomat的发布目录C:\Tomcat\webapps\ROOT下就可以了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

Steps to perform SQL in Navicat: Connect to the database. Create a SQL Editor window. Write SQL queries or scripts. Click the Run button to execute a query or script. View the results (if the query is executed).

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".
