[Java EE] How to configure the Java EE Development environment in the Linux environment with Eclipse + Tomcat + MySQL

Source: Internet
Author: User
Tags server port tomcat server

Http://www.colabug.com/thread-1168296-1-1.html

1. Version information

(1) CentOS 6.4 release 64-bit, uname-a display as follows:

Linux localhost.localdomain 3.11.6 #1 SMP Sat 2 23:25:40 KST x86_64 x86_64 x86_64 gnu/linux
(2) Eclipse:Version:Kepler Service Release 2
(3) tomcat:apache-tomcat-7.0.53
(4) mysql:mysql-server-5.1.73
(5) JDBC driver:mysql-connector-java-5.1.30
2. Install the software (most operations require root or sudo permissions)
(1) Install Eclipse, see the previous "Linux Shell Environment manual installation of Eclipse" (http://blog.csdn.net/kleguan/article/details/25873997)
(2) Install Tomcat, see the previous article, "Configuring Tomcat to allow the specified user Management Service (Linux platform)" (http://blog.csdn.net/kleguan/article/details/25902495)
Note: When installing the JDK, it is not necessary to use the method described in this article, just configure the environment variable (java_home,path,classpath) in the correct way.
(3) Install MySQL, use the system's own installation kit, yum install mysql-* after installation, use Rpm-qa | grep MySQL should include all of the following components.

Mysql-server-5.1.73-3.el6_5.x86_64
Mysql-libs-5.1.73-3.el6_5.x86_64
Mysql-5.1.73-3.el6_5.x86_64
Mysql-devel-5.1.73-3.el6_5.x86_64
(4) Installing the JDBC Driver
http://dev.mysql.com/downloads/file.php?id=451546
After executing tar-xvzf mysql-connector-java-5.1.30.tar.gz, copy the jar file into the Tomcat library.
CP mysql-connector-java-5.1.30-bin.jar/opt/tomcat7/lib/
Note: The jar file can also be copied to the Lib of a specific project for use by a specific project.
3. Configuration method (most operations require root or sudo permissions)
(1) Create a MySQL database and authorize the specified user.
Mysqladmin-u root-p Create Db_name
Grant on db_name to [identified by User-password] [with GRANT option];
(2) Modifying the Tomcat configuration file
Locate the Tomcat/conf/context.xml file in the Tomcat installation directory, and VI context.xml add the following statement.

Note: If the MySQL server port is not the default port, you should add:p ort_no after the DB URL
(3) Open Eclipse and add the Tomcat server, as shown in.

You can use the Tomcat server when you are done adding.
4. Invocation mode

  1. Import Javax.naming.Context;
  2. Import Javax.naming.InitialContext;
  3. Import javax.naming.NamingException;
  4. Import Javax.sql.DataSource;
  5. Import java.sql.*;
  6. public class DBAccess {        private static DataSource ds=null;        Private C Ontext ctx=null;        Private Connection conn=null;        Statement stat=null;& nbsp       Private ResultSet rs=null;        public DBAccess () throws Namingexception {ctx= New InitialContext (); Ds= (DataSource) ctx.lookup ("Java:/comp/env/jdbc/mysql");       }        Public void Daoclose () throws sqlexception{        if (rs!=null) {        rs.close (); &nbsp ;      }        if (stat!=null) {        stat.close ();    & nbsp   IF (conn! = null) {        conn.close ();}        }        public int executeupdate (String sql) throws sqlexception{  & nbsp     INT status=-1;  &nbsp     try{        conn=ds.getconnection ();        Conn.setautocommit ( False);        stat =conn.createstatement ();        Status=stat.executeupdate ( SQL);        conn.commit ();       }finally{        Daoclose () ;       }        return status;       }
  7. }
Copy Code

The way to connect to the database is very intuitive, using the Intialcontext function to read the contents of Context.xml, and then establish a database connection pool, where needed to instantiate a database connection and use.

[Java EE] How to configure the Java EE Development environment in the Linux environment with Eclipse + Tomcat + MySQL

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.