JDK 在 linux 下安裝
1. 把安裝文件放在 /opt 下,并執(zhí)行
[root@localhost opt]# ./jdk-1_5_0_06-linux-i586.bin
|
并輸入 yes 確認安裝
2. 創(chuàng)建 /etc/profile.d/java.sh 編輯環(huán)境變量
export JAVA_HOME=/opt/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
|
并執(zhí)行
[root@localhost profile.d]# source java.sh
|
3. 驗證 輸入 javac 出現(xiàn)提示信息
4. 執(zhí)行 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!!!");
}
}
|
執(zhí)行結(jié)果
[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/下的權(quán)限,使其具有執(zhí)行的權(quán)限
[root@localhost tomcat]# chmod 755 -R bin/
|
3. 啟動 tomcat
[root@localhost bin]# ./startup.sh
|
結(jié)果:
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 如出現(xiàn) TOMCAT 初始界面說明配置成功。
如果出現(xiàn)其他頁面,則端口被占用,需要修改 TOMCAT 的端口號。
MYSQL 在Linux下的安裝
(1) 執(zhí)行"groupadd mysql"添加組mysql用戶組。在mysql 組下增加 mysql 用戶
[root@localhost etc]# groupadd mysql
[root@localhost etc]# useradd -g mysql mysql
|
執(zhí)行后查看確認
[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 (在解壓的目錄下執(zhí)行 configure 程序 設置安裝路徑 )
[root@localhost mysql-5.1.30]# ./configure -prefix=/opt/mysql
|
(4)出現(xiàn)Thank you for choosing MySQL! 執(zhí)行make 進行編譯 再執(zhí)行 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 的身份啟動數(shù)據(jù)庫
[root@localhost bin]./bin/mysqld_safe --user=mysql
|
結(jié)果:
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)驗證數(shù)據(jù)庫的安裝 (默認安裝后 root 是沒有密碼的 進入數(shù)據(jù)庫直接回車)
[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. 查看數(shù)據(jù)庫
在進入數(shù)據(jù)庫后輸入 show databases; ( 注意分號 )
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| redmoonoa |
| test |
+--------------------+
4 rows in set (0.07 sec)
|
3. 進入數(shù)據(jù)庫
mysql> use test ;
Database changed
|
4. 查看數(shù)據(jù)庫中的表
mysql> show tables;
Empty set (0.00 sec)
|
5. 建數(shù)據(jù)庫、表、
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 // 查看表結(jié)構(gòu)
-> ;
+-------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+----------------+
| id | int(3) | NO | PRI | NULL | auto_increment |
| name | char(10) | NO | | NULL | |
| pwd | char(10) | NO | | NULL | |
+-------+----------+------+-----+---------+----------------+
|
插入數(shù)據(jù):
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 頁面, 驗證 數(shù)據(jù)庫,服務器 環(huán)境配置的正確。
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 驅(qū)動
下載相應 JDBC 驅(qū)動
mysql-connector-java-3.1.8-bin.jar
把驅(qū)動放在 sql_test 下建立文件夾 /WEB-INF/lib 下
3. 運行程序
在瀏覽器中,輸入 http://127.0.0.1:8080/sql_test/test.jsp
出現(xiàn)結(jié)果
username:user1 password:123456
username:admin password:123456
證明系統(tǒng)運行正常
下面是程序文件,可以直接放在tomcat的 webapps 下運行
|