jsp to connect to the database — Mysql select from the NinGoo Blog

Operating System: windows XP SP1

1 Install the JDK 5.0 Update 1

Download:

Assuming that the installation path is D :/ Java/jdk1.5.0_01
Set the environment variable (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

Downloads:
( )

Installed to d :/ tomcat 5.5, Tomcat 5.5 is installed need JRE 5.0 installation process does not automatically identify the first installed jdk5.0 path, you need to manually specify the path of the JRE5.0.

Set the environment variable
CATALINA_HOME = D :/ Tomcat 5.5

Tomcat in the new virtual directory test to D plus one new :/ Tomcat 5.5/conf/Catalina/localhost, test.xml
Reads as follows:
<Context Path=”/test” docBase=”d:/www” reloadable=”true” crossContext=”true” debug=”0″>
</ Context>

d :/ www path will be our test site’s root directory

In your browser by access

3 Install Mysql4.1.8

Downloads:

Installed to D :/ MySQL, create a new test db

4 Installing the JDBC Driver MySQL Connector / J 3.1.6

Downloads:

Unzip one of the mysql-connector-java-3.1.6-bin.jar copied to an the D :/ the Tomcat 5.5/common/lib or D :/ Java/jdk1.5.0_01/jre/lib/ext or an other place (the need to set the classpath to point to the path)

5 New test.jsp

Save to D :/ www

Reads 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: <% = rs.getString (1)%>
<%}%>
<% Out.print (“database operations successful, congratulate you / 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 ());
}
%>