JSP環(huán)境搭建之MySQL、JDBC的安裝與測試系統(tǒng)
JSP環(huán)境搭建第三步、MYSQL的安裝
1)、安裝MYSQL
- [root@linuxas Server]# rpm -vih perl-DBI-1.52-1.fc6.i386.rpm
 - warning: perl-DBI-1.52-1.fc6.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
 - Preparing... ########################################### [100%]
 - 1:perl-DBI ########################################### [100%]
 - [root@linuxas Server]# rpm -vih mysql-5.0.22-2.1.i386.rpm
 - warning: mysql-5.0.22-2.1.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
 - Preparing... ########################################### [100%]
 - 1:mysql ########################################### [100%]
 - [root@linuxas Server]# rpm -vih perl-DBD-MySQL-3.0007-1.fc6.i386.rpm
 - warning: perl-DBD-MySQL-3.0007-1.fc6.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
 - Preparing... ########################################### [100%]
 - 1:perl-DBD-MySQL ########################################### [100%]
 - [root@linuxas Server]# rpm -vih mysql-server-5.0.22-2.1.i386.rpm
 - warning: mysql-server-5.0.22-2.1.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
 - Preparing... ########################################### [100%]
 - 1:mysql-server ########################################### [100%]
 
2)、啟動服務(wù)
- [root@linuxas ~]# service mysqld start
 
初始化 MySQL 數(shù)據(jù)庫:
- Installing all prepared tables
 - Fill help tables
 - To start mysqld at boot time you have to copy support-files/mysql.server
 - to the right place for your system
 - PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
 - To do so, start the server, then issue the following commands:
 - /usr/bin/mysqladmin -u root password 'new-password'
 - /usr/bin/mysqladmin -u root -h linuxas password 'new-password'
 - See the manual for more instructions.
 - You can start the MySQL daemon with:
 - cd /usr ; /usr/bin/mysqld_safe &
 - You can test the MySQL daemon with the benchmarks in the 'sql-bench' directory:
 - cd sql-bench ; perl run-all-tests
 - Please report any problems with the /usr/bin/mysqlbug script!
 - The latest information about MySQL is available on the web at
 - http://www.mysql.com
 - Support MySQL by buying support/licenses at http://shop.mysql.com
 
[確定]
啟動 MySQL: [確定]
設(shè)置登錄 MySQL 的用戶名和密碼
- [root@linuxas ~]# mysqladmin -uroot password pwd@123
 
運(yùn)行mysql ,并開放root用戶的遠(yuǎn)程訪問權(quán)限。以便調(diào)試
- use mysql
 - update user set host = '%' where user = 'root' and host < > 'localhost';
 - flush privileges;
 - quit
 
JSP環(huán)境搭建第四步、JDBC的安裝
1)、從官網(wǎng)下載安裝程http://dev.mysql.com/downloads/connector/j/5.1.html下載***文件mysql-connector-java-5.1.7.tar.gz并上傳到安裝目錄。
2)、安裝JDBC并配置tomcat
- [root@linuxas src]# tar -zxvf mysql-connector-java-5.1.7.tar.gz
 - [root@linuxas src]# cp mysql-connector-java-5.1.7/mysql-connector-java-5.1.7-bin.jar /tomcat/lib/
 - [root@linuxas src]# cp mysql-connector-java-5.1.7/mysql-connector-java-5.1.7-bin.jar /usr/local/src/jdk1.6.0_14/jre/lib/
 
JSP環(huán)境搭建第五步、測試系統(tǒng)環(huán)境
1)、新建數(shù)據(jù)庫及表
- mysql> GRANT ALL PRIVILEGES ON *.* TO llk726@localhost IDENTIFIED BY 'pwd@123' WITH GRANT OPTION;
 - mysql> create database test;
 - mysql> use test;
 - mysql> create table testtable(id int not null auto_increment primary key, foo varchar(25), bar int);
 - mysql> insert into testtable values(null, 'hello', 12345);
 
2)、編寫測試網(wǎng)頁test.jsp
- [root@linuxas ROOT]# vi test.jsp
 - < %@ page contentType="text/html; charset=gb2312" language="java"%>
 - < %@page import="java.sql.*"%>
 - < %
 - Class.forName("com.mysql.jdbc.Driver").newInstance();
 - String url="jdbc:mysql://localhost/test?user=llk726&password=pwd@123&useUnicode=true&characterEncoding=utf8";
 - Connection conn=DriverManager.getConnection(url);
 - Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
 - String sql="select * from testtable";
 - %>
 - < table border=1>
 - < tr>
 - < td>字段1< /td>
 - < td>字段2< /td>
 - < td>字段3< /td>
 - < /tr>
 - < %
 - ResultSet rs=stmt.executeQuery(sql);
 - while(rs.next()){%>
 - < tr>
 - < td>< %=rs.getString("ID")%> < /td>
 - < td>< %=rs.getString("FOO")%> < /td>
 - < td>< %=rs.getString("BAR")%> < /td>
 - < /tr>
 - < %}%>
 - < /table>
 - < %rs.close();
 - stmt.close();
 - conn.close();
 - %>
 
3)、測試結(jié)果如下:
JSP的運(yùn)行環(huán)境到此便搭建成功。本文出自 “鍵盤系人生” 博客。
【編輯推薦】
















 
 
 

 
 
 
 