JSP Connection Database---Mysql

Source: Internet
Author: User
Tags mysql variables stmt root directory tomcat
js|mysql| Connection Database JSP connection database---MySQL

Time: 2005-1-24
Operating system: Windows XP SP1

1. Install JDK 5.0 Update 1

Download: http://java.sun.com/

Suppose the installation path is D:\Java\jdk1.5.0_01
Setting environment variables (Control Panel-> System-> Advanced)
Java_home=d:\java\jdk1.5.0_01
classpath=.;D: \ Java\jdk1.5.0_01\lib\dt.jar;d:\java\jdk1.5.0_01\lib\tools.jar;
Path=path;%java_home%\bin

2. Install Tomcat 5.5.4

Download: http://jakarta.apache.org/site/binindex.cgi
(Http://apache.freelamp.com/jakarta/tomcat-5/v5.5.4/bin/jakarta-tomcat-5.5.4.exe)

Installing to D:\tomcat 5.5, installing Tomcat 5.5 requires JRE 5.0, and if you do not automatically identify the path of the first installed jdk5.0 during installation, you need to manually specify the JRE5.0 path.

Setting environment variables
Catalina_home=d:\tomcat 5.5

In Tomcat, add a new virtual directory test to D:\Tomcat 5.5\conf\catalina\localhost and add a Test.xml
The contents are as follows:
<context path= "/test" docbase= "D:/www" reloadable= "true" crosscontext= "true" debug= "0" >
</Context>

The d:\www path will be the root directory of our test site

Accessing through Http://localhost:8080/test in the browser

3. Install Mysql4.1.8

Download: http://dev.mysql.com/downloads/index.html

Install to D:\MySQL, create a new db for test

4. Install JDBC driver MySQL connector/j 3.1.6

Download: http://dev.mysql.com/downloads/index.html

After decompression, copy the Mysql-connector-java-3.1.6-bin.jar to D:\Tomcat 5.5\common\lib or D:\Java\jdk1.5.0_01\jre\lib\ext or elsewhere ( You need to set the classpath to point to the path)

5. A new test.jsp

Save to D:\www

The contents are as follows:
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.sql.*"%>
<%

Statement Stmt=null;
ResultSet rs = null;

Class.forName ("Com.mysql.jdbc.Driver"). newinstance ();

try {
Connection conn = drivermanager.getconnection ("jdbc:mysql://localhost/test?user=root&password=sea1980");
stmt = Conn.createstatement ();
rs = Stmt.executequery ("SELECT * from Test");

while (Rs.next ()) {%>
Your first field content is: <%=rs.getstring (1)%>
<%}%>
<%out.print ("Database operation successful, congratulations \ \ \ \ n");%>
<%rs.close ();
Stmt.close ();
Conn.close ();
catch (SQLException ex) {
Handle any errors
System.out.println ("SQLException:" + ex.getmessage ());
System.out.println ("SQLState:" + ex.getsqlstate ());
System.out.println ("Vendorerror:" + ex.geterrorcode ());
}
%>



Author blog:http://blog.csdn.net/ningoo/


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.