Linux 下搭建 Tomcat+MySQL+JSP
安装环境为:CentOS-6.5-x86_64-minimal
0x00 安装mysql
$ sudo yum install mysql mysql-server mysql-devel
0x01 安装jdk和tomcat
-
创建安装jdk和tomcat的文件夹
-
下载安装包并解压安装
jdk包可在这里下载
tomcat包可在这里下载
为java添加环境变量并设置命令链接符
$ tail -n 3 /etc/profile
export JAVA_HOME="/opt/jdk1.8.0_91"
export CLASSPATH=.:$JAVA_HOME/lib
export PATH=$JAVA_HOME/bin:$PATH
$ sudo update-alternatives --install /usr/bin/java java /opt/jdk1.8.0_91/bin/java 1
$ sudo update-alternatives --install /usr/bin/javac javac /opt/jdk1.8.0_91/bin/javac 1
$ sudo update-alternatives --set java /opt/jdk1.8.0_91/bin/java
$ sudo update-alternatives --set javac /opt/jdk1.8.0_91/bin/javac
0x02 简单配置tomcat
0x03 启动tomcat
0x04 连接数据库
驱动可在这里下载 $ vim /opt/tomcat/apache-tomcat-8.0.36/webapps/ROOT/a.jsp
<%@page contentType="text/html;charset=utf-8" import="java.sql.*" %>
<%
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/tomcat"; //数据库web
String user = "tomcat";
String password = "123456";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed())
out.println("数据库连接成功!");
conn.close();
}
catch(ClassNotFoundException e) {
out.println("找不到驱动程序");
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
%>