mysql|servlet| Access
Row Environment: JDK 5.0 + Tomcat 5.5.4
JDK Installation path: D:\Java\jdk1.5.0_01
Tomcat installation path: D:\Tomcat 5.5
Setting environment variables (Control Panel-> System-> Advanced):
Java_home=d:\java\jdk1.5.0_01
Path=%java_home%\bin
classpath=.;D: \ Java\jdk1.5.0_01\lib\dt.jar;d:\java\jdk1.5.0_01\lib\tools.jar;d:\tomcat 5.5\common\lib\servlet-api.jar;d:\ Tomcat 5.5\common\lib\mysql-connector-java-3.1.6-bin.jar;
Where D:\Tomcat 5.5\common\lib\servlet-api.jar is implemented for Tomcat's Servlet API, be sure to add it, so you can't compile the servlet
D:\Tomcat 5.5\common\lib\mysql-connector-java-3.1.6-bin.jar is the JDBC driver for MySQL, connect/j 3.1.6, can be downloaded to the official MySQL website
Tomcat5.5 Virtual directory settings:
D:\Tomcat 5.5\conf\catalina\localhost under the new 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 Web site, accessed through Http://localhost:8080/test access to the virtual directory
D:\www under the directory Web-inf, under the Web.xml settings file (refer to D:\Tomcat 5.5\webapps\root\web-inf\web.xml), classes directory and Lib directory
Java Bean file name for connection database Dbconn.java
Package Ningoo;
Import java.sql.*;
public class Dbconn {
Public Dbconn () {
}
DECLARE variable
PRIVATE Connection conn = null;
ResultSet rs = null;
Private String Server = "127.0.0.1";
Private String port = "3306";
Private String db = "Test";
Private String user = "root";
Private String pass = "password";
Private String drivername= "Com.mysql.jdbc.Driver";
Private String url= "jdbc:mysql://" "+server+": "+port+"/"+db+"? user= "+user+" &password= "+pass";
Public Connection Getconn () {//get database Connection
try{
Class.forName (drivername). newinstance ();
conn = Drivermanager.getconnection (URL);
}
catch (Exception e) {
E.printstacktrace ();
}
return this.conn;
}
Public ResultSet ExecuteSQL (String str) {
try{
Statement stmt = Conn.createstatement ();
rs = stmt.executequery (str);
}
catch (Exception e) {
E.printstacktrace ();
}
return this.rs;
}
}
Compile Javac Welcome.java, put the compiled file Welcome.class under the directory D:\www\WEB-INF\classes\NinGoo\
Invoke the Java Bean's servlet Welcome.java
Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.sql.*;
public class Welcome extends HttpServlet {
Public welcome () {
}
public void doget (HttpServletRequest req,httpservletresponse Res) throws Servletexception,ioexception {
Res.setcontenttype ("text/html");
Connection Conn=null;
Statement Stmt=null;
ResultSet rs = null;
try{
NinGoo.database.dbconn myconn =new NinGoo.database.dbconn ();
conn = Myconn.getconn ();
rs = Myconn.executesql ("SELECT * from Test"); Test is a table in MySQL
PrintWriter Out=res.getwriter ();
Out.println ("Out.println ("<body>");
while (Rs.next ()) {
Out.println (rs.getstring (1));
}
Out.println ("successfule!</body>}
catch (Exception e) {
}
}
}
Compile Javac Welcome.java, put the compiled file Welcome.class under the directory D:\www\WEB-INF\classes
Then modify the D:\www\WEB-INF\web.xml and add the following settings
<servlet>
<servlet-name>welcome</servlet-name>
<servlet-class>welcome</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
Restart Tomcat after saving, and then tap Http://localhost:8080/test/welcome in the browser
Author blog:http://blog.csdn.net/ningoo/
Related articles
accessing MySQL using servlet and Java beans
Jsp+java beans access to MySQL database
Writing the first servlet
How to: Transfer logins and passwords between instances of SQL Server
JSP Connection Database---Mysql