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
- Import Javax.naming.Context;
- Import Javax.naming.InitialContext;
- Import javax.naming.NamingException;
- Import Javax.sql.DataSource;
- Import java.sql.*;
- 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 ();   ; } if (stat!=null) { stat.close (); & nbsp IF (conn! = null) { conn.close ();} } public int executeupdate (String sql) throws sqlexception{ & nbsp INT status=-1;   try{ conn=ds.getconnection (); Conn.setautocommit ( False); stat =conn.createstatement (); Status=stat.executeupdate ( SQL); conn.commit (); }finally{ Daoclose () ; } return status; }
- }
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