windows下jsp+mysql网站环境配置方法
2024-07-24 12:41:24
供稿:网友
数据库采用mysql(配置过程如有问题可以重启tomcat试一下)
一、下载软件
.J2SE
http://java.sun.com/j2se/.4.2/download.html
2.Tomcat服务器
http://tomcat.apache.org/download-4.cgi
3.mysql服务器
http://dev.mysql.com/downloads/mysql/5.0.html
安装时字符集选latin(应与数据库采用字符集相同)
安装好数据库后用以下命令看数据库采用什么字符集:
help
4.JDBC驱动
http://dev.mysql.com/downloads/connector/j/5.0.html
二、依次安装以上软件
假设安装后的路径分别如下:
.J2SE
C:/j2sdk.4.2
2.Tomcat
C:/Program Files/Apache Software Foundation/Tomcat 4.
3.mysql
C:/Program Files/MySQL/MySQL Server 5.0
三、配置
.设置环境变量
在'我的电脑'上点右键,属性->高级->环境变量,添加如下变量:
PATH=%PATH%;C:/j2sdk.4.2/bin;C:/Program Files/MySQL/MySQL Server 5.0/bin
JAVA_HOME=C:/j2sdk.4.2
CLASSPATH=C:/j2sdk.4.2/lib/tools.jar;mysql-connector-java-5.0.5-bin.jar;mysql-connector-java-5.0.5-bin-g.jar
TOMCAT_HOME=C:/Program Files/Apache Software Foundation/Tomcat 4.
2.jsp连接mysql设置
把
mysql-connector-java-5.0.5-bin.jar
mysql-connector-java-5.0.5-bin-g.jar
拷贝到C:/j2sdk.4.2/lib
把
mysql-connector-java-5.0.5-bin.jar
拷贝到
C:/Program Files/Apache Software Foundation/Tomcat 4./common/lib
C:/Program Files/Apache Software Foundation/Tomcat 4./shared/lib
(千万别复制mysql-connector-java-5.0.5-bin-g.jar)
四、JSP连接mysql测试
.建立数据库xia
打开命令行窗口,输入:
mysql -h localhost -u root -p
crate database xia;
use xia;
crate table member(id int(8) primary key,name varchar(0));
insrt into member values(,'yang');
insrt into member(name,id) values('xia',2);
(安全:为数据库设置权限(用户和密码)
命令:grant all privileges on shujuku.* to test@localhost identified by “23456”;
当你执行完这个命令以后,只要你再以用户名:test,密码:23456登录时你就只可以对shujuku这个数据库操作,
这样避开使用root,对数据库的安全有很大帮助.)
2.把以下文件保存为index.jsp到C:/Program Files/Apache Software Foundation/Tomcat 4./webapps/ROOT
----------------begin----------------------
复制代码 代码如下:
<%@ page contentType="text/html; charset=gb232" %>
<%@ page language="java" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %>
<%
//驱动程序名
String driverName="com.mysql.jdbc.Driver";
//数据库用户名
String userName="root";
//密码
String userPasswd="";
//数据库名
String dbName="xia";
//表名
String tableName="member";
//联结字符串
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.crateStatement();
String sql="Selct * FROM "+tableName;
ResultSet rs = statement.executeQuery(sql);