Where D:\Tomcat 5.5\common\lib\mysql-connector-java-3.1.6-bin.jar is the JDBC driver of 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";
Compile Javac Welcome.java, put the compiled file Welcome.class under the directory D:\www\WEB-INF\classes\NinGoo\
Call the Java Bean's JSP file test.jsp
<%@ page contenttype= "text/html;charset=gb2312" import= "java.sql.*"%>
<jsp:usebean id= "Ningoo" scope= "page" class= "Ningoo.dbconn"/>
<%
ResultSet rs = null;
Connection conn = null;
Ningoo.setserver ("127.0.0.1"); Set MySQL server name or IP
Ningoo.setport ("3306"); Set up the listening port for MySQL
Ningoo.setdb ("test"); Set MySQL database name
Ningoo.setuser ("root"); Set user name to connect to MySQL
Ningoo.setpass ("password"); Set the password to connect to MySQL
conn = Ningoo.getconn ();
rs = Ningoo.executesql ("SELECT * from Test");
while (Rs.next ()) {
%>
Row:<%=rs.getstring (1)%>
<%}%>
<%out.print ("successful!\n");%>
<%
Rs.close ();
Conn.close ();
%>
Then in the browser to visit http://localhost:8080/test/test.jsp, congratulations success!
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.