Eclipse + Tomcat for MySQL database connection

Source: Internet
Author: User

Note: This article assumes that you have successfully installed the Tomcat plug-in sysdeo of eclipse!

First, create a tomcat project: file-> New-> Other and select the Tomcat project (figure 1)
Enter the project name as connmysql (figure 2)
For convenience, select can update server. xml file (default) and click Finish to create the project file (Figure 3). Create a Java file in the connmysql project. Select connmysql from File> New and click class (Figure 4). In the package, enter test and name as sqlbean, and the others as default. The file is created. Sqlbean File Code As follows:
Package test;
Import java. SQL. connection;
Import java. SQL. drivermanager;
// Sqlbean is an abstract function for data connection and shutdown.
Public abstract class sqlbean {
Private string mydriver = "org. gjt. Mm. MySQL. Driver ";
Private string myurl = "JDBC: mysql: // 8.8.8.2: 3306/netbilling? User = flux & Password = 123456 ";

Protected connection myconn;
Public sqlbean (){}

Public void makeconnection () throws exception {
Class. forname (mydriver). newinstance ();
Myconn = drivermanager. getconnection (myurl );
}

Public abstract void cleanup () throws exception;
Public void takedown () throws exception {
Cleanup ();
Myconn. Close ();
}
}
Create a dbbean file in the same way. The dbbean file code is as follows:
Package test;

Import Java. SQL. resultset;
Import Java. SQL. statement;
// dbbean inherits data from sqlbean for data retrieval
public class dbbean extends sqlbean {

String MySQL = "select operatorno, operatorname, description from operator ";
Resultset myresuleset = NULL;
Statement stmt = NULL;
Public dbbean (){
Super ();
}
 
Public Boolean getnextdb () throws exception {
Return myresuleset. Next ();
}
 
Public String getcolumn (string incol) throws exception {
Return myresuleset. getstring (incol );
}
 
Public Boolean getdata () throws exception {
String myquery = MySQL;
Stmt = myconn. createstatement ();
Myresuleset = stmt.exe cutequery (myquery );
Return (myresuleset! = NULL );
}
 
Public void cleanup () throws exception {
Stmt. Close ();
}
}
Next, create the JSP file that calls this bean. In this case, you must select the project file connmysql and then create the JSP file, so that the file is directly created under the connmysql directory for convenient calling. File-> New-> file (figure 5)

The code of the JSP file dbquery. jsp is:
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<% @ Page Language = "Java" %>
<% @ Page import = "Java. SQL. *" %>
<% @ Page contenttype = "text/html; charset = gb2312" %>
<JSP: usebean id = "dbbean" class = "test. dbbean" Scope = "page"/>
<Title> lomboz JSP </title>
</Head>
<Body bgcolor = "# ffffff">
<Table border = "1" width = "400">
<Tr>
<TD> <B> NO </B> </TD> <B> name </B> </TD> <B> description </B> </TD>
</Tr>
<%
Dbbean. makeconnection ();
If (dbbean. getdata ()){
While (dbbean. getnextdb ()){
String NO = dbbean. getcolumn ("operatorno ");
String name = dbbean. getcolumn ("operatorname ");
String DESC = dbbean. getcolumn ("Description ");
%>
<Tr>
<TD> <% = No %> </TD>
<TD> <% = Name %> </TD>
<TD> <% = DESC %> </TD>
</Tr>
<%
}
}
Dbbean. Takedown ();
%>
</Table>
</Body>
</Html>
The basic code has been completed, but there is still a very important step, because this Program So we also need to copy the jdbcmysql. jar file to the WEB-INF directory (automatically created by the eclipse Program), otherwise the classnotfound error message will appear when calling the JSP file. At the beginning, I thought eclipse was very intelligent to include the file, Because I included the JAR file in liberaries of the project. Who knows the result is not like this, and I was depressed for a long time.
Okay, save the file, and click the tiger icon on the eclipse interface to start Tomcat (Figure 6). In the browser, enter http: // localhost: 8080/connmysql/dbquery. JSP, haha, success! (Figure 7)

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 73568

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.