您的位置:首页 > 运维架构 > Tomcat

linux 下 jdk+tomcat+mysql 的 jsp 环境搭建

2014-09-29 01:33 671 查看

JDK 在 linux 下安装

1. 把安装文件放在 /opt 下,并执行

[root@localhost opt]# ./jdk-1_5_0_06-linux-i586.bin

并输入 yes 确认安装

2. 创建 /etc/profile.d/java.sh 编辑环境变量

export JAVA_HOME=/opt/jdk

export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

并执行

[root@localhost profile.d]# source java.sh

3. 验证 输入 javac 出现提示信息

4. 执行 helloworld 小程序

在 /opt/test 下建立 HelloWorld.java

public class HelloWorld{

public static void main(String args[]){

HelloWorld hello=new HelloWorld();

hello.printHello();

}

private void printHello(){

System.out.println("hello!!!");

}

}

执行结果

[root@localhost test]# javac HelloWorld.java

[root@localhost test]# java HelloWorld

hello!!!

TOMCAT 在 Linux 下的安装

1. 把安装文件放在 /opt 下,并且解压文件

[root@localhost opt]# unzip apache-tomcat-5.5.28.zip

2. 修改安装文件下的 bin/下的权限,使其具有执行的权限

[root@localhost tomcat]# chmod 755 -R bin/

3. 启动 tomcat

[root@localhost bin]# ./startup.sh

结果:

Using CATALINA_BASE: /opt/tomcat

Using CATALINA_HOME: /opt/tomcat

Using CATALINA_TMPDIR: /opt/tomcat/temp

Using JRE_HOME: /opt/jdk

Using CLASSPATH: /opt/tomcat/bin/bootstrap.jar

4. 验证tomcat 安装成功

在浏览器中输入http://localhost:8080 如出现 TOMCAT 初始界面说明配置成功。

如果出现其他页面,则端口被占用,需要修改 TOMCAT 的端口号。

MYSQL 在Linux下的安装

(1) 执行"groupadd mysql"添加组mysql用户组。在mysql 组下增加 mysql 用户

[root@localhost etc]# groupadd mysql

[root@localhost etc]# useradd -g mysql mysql

执行后查看确认

[root@localhost etc]# cd /home/

[root@localhost home]# ll

total 32

drwx------ 2 root root 16384 Oct 25 2009 lost+found

drwx------ 2 lto lto 4096 Oct 25 2009 lto

drwx------ 2 mysql mysql 4096 Oct 25 01:01 mysql

(2) 在opt 目录下解压mysql安装文件

[root@localhost opt]# tar -zxvf mysql-5.0.0-alpha.tar.gz

(3) 安装mysql (在解压的目录下执行 configure 程序 设置安装路径 )

[root@localhost mysql-5.1.30]# ./configure -prefix=/opt/mysql

(4)出现Thank you for choosing MySQL! 执行make 进行编译 再执行 make install 安装

[root@localhost mysql-5.1.30]# make

[root@localhost mysql-5.1.30]# make install

(5)以mysql 的身份进行安装

[root@localhost bin]# ./mysql_install_db --user=mysql

(6)修改相应文件的主属,和组属

[root@localhost bin]# chown -R root .

[root@localhost bin]# chown -R mysql var

[root@localhost bin]# chgrp –R mysql .

(7)以 mysql 的身份启动数据库

[root@localhost bin]./bin/mysqld_safe --user=mysql

结果:

091025 23:50:49 mysqld_safe Logging to '/opt/mysql/var/localhost.localdomain.err'.

091025 23:50:50 mysqld_safe A mysqld process already exists

(8)验证数据库的安装 (默认安装后 root 是没有密码的 进入数据库直接回车)

[root@localhost bin]# ./mysql -u root -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 5

Server version: 5.1.30 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

Mysql 的基本操作

1. 修改用户密码

因为 root 用户初始没有密码 所以直接修改

[root@localhost bin]# ./mysqladmin -u root -password 123456

为已有密码的用户修改密码

[root@localhost bin]# ./mysqladmin -u root -p111111 password 123456

2. 查看数据库

在进入数据库后输入 show databases; ( 注意分号 )

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| redmoonoa |

| test |

+--------------------+

4 rows in set (0.07 sec)

3. 进入数据库

mysql> use test ;

Database changed

4. 查看数据库中的表show

mysql> show tables;

Empty set (0.00 sec)

5. 建数据库、表、

mysql> create database mysql_test

-> ;

Query OK, 1 row affected (0.02 sec)

mysql> use mysql_test;

Database changed

mysql> create table users ( id int(3) auto_increment not null primary key, namechar(10) not null, pwd char(10) not null);

Query OK, 0 rows affected (0.04 sec)

验证:

mysql> describe users // 查看表结构

-> ;

+-------+----------+------+-----+---------+----------------+

| Field | Type | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+----------------+

| id | int(3) | NO | PRI | NULL | auto_increment |

| name | char(10) | NO | | NULL | |

| pwd | char(10) | NO | | NULL | |

+-------+----------+------+-----+---------+----------------+

插入数据:

mysql> insert into users(name,pwd) values('user1','123456');

Query OK, 1 row affected (0.00 sec)

mysql> insert into users(name,pwd) values('admin','123456');

Query OK, 1 row affected (0.01 sec)

验证:

mysql> select * from users

-> ;

+----+-------+--------+

| id | name | pwd |

+----+-------+--------+

| 1 | user1 | 123456 |

| 2 | admin | 123456 |

+----+-------+--------+

2 rows in set (0.00 sec)

在 linux 下搭建 jdk+tomcat+mysql 应用

通过一个简单的 jsp 页面, 验证 数据库,服务器 环境配置的正确。

1. 编写 JSP 程序

在 tomcat/wapapps 文件夹下,建立文件夹 sql_test

在 sql_test 下建立文件 test.jsp

在 test.jsp 中敲入下面程序

<%@ page contentType="text/html; charset=gb2312" language="java"

import="java.sql.*"%>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<%

String server="localhost";

String dbname="mysql_test";

String user="root";

String pass="123456";

String port="3306";

String url ="jdbc:mysql://"+server+":"+port+"/"+dbname+"?user="+user+"&password="+pass;

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

Connection conn= DriverManager.getConnection(url);

Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

String sql="select name,pwd from users";

ResultSet rs=stmt.executeQuery(sql);

while(rs.next()){

out.print("username:");

out.print(rs.getString("name")+" password:");

out.println(rs.getString("pwd")+"<br>");

}

rs.close();

stmt.close();

conn.close();

%>

2. 配置 JDBC 驱动

下载相应 JDBC  驱动 

mysql-connector-java-3.1.8-bin.jar

把驱动放在  sql_test 下建立文件夹 /WEB-INF/lib 下

3. 运行程序

在浏览器中,输入 http://127.0.0.1:8080/sql_test/test.jsp
出现结果

username:user1 password:123456

username:admin password:123456

证明系统运行正常

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: