1. Version number information
(1) CentOS 6.4 release 64 position. Uname-a The following example shows a significant sample:
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 (requires root or sudo permissions for most operations)
(1) Install Eclipse, see the previous "Linux Shell environment Manually install Eclipse" (http://blog.csdn.net/kleguan/article/details/25873997)
(2) Install Tomcat, see the previous article, "Configuring Tomcat, agreeing to specify User management services (Linux platform)" (http://blog.csdn.net/kleguan/article/details/25902495)
Note: The method described in this article is not required when installing the JDK. Just configure the environment variable (java_home,path,classpath) in the correct way.
(3) Install MySQL, use the system comes with the installation kit can be, yum install mysql-* after installation, use Rpm-qa | grep MySQL should contain 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
id=451546 "target=" _blank ">http://dev.mysql.com/downloads/file.php?id=451546
After running Tar-xvzf mysql-connector-java-5.1.30.tar.gz, copy the jar files into the Tomcat library.
CP mysql-connector-java-5.1.30-bin.jar/opt/tomcat7/lib/
Note: You can also copy the jar file to the Lib of a specific project for use by specific project.
3. Configuration method (requires root or sudo permissions for most operations)
(1) Create a MySQL database and authorize the specified user.
Mysqladmin-u root-p Create db_name Grant <privilege> on db_name to <user> [identified by User-password] [with GRANT option];
(2) Changing the Tomcat configuration file
Locate the Tomcat/conf/context.xml file under the Tomcat installation folder, and vi context.xml add for example the following statement.
<resource name= "Jdbc/mysql" auth= "Container" type= "Javax.sql.DataSource" maxactive= "" maxidle= "" maxwait= " "Username=" Db_username "password=" DB_USERPW "driverclassname=" Com.mysql.jdbc.Driver "url=" jdbc:mysql:// Localhost/db_name "/>
Note: Assuming that mysqlserverport is not the default port, you should add:p after the DB URL ort_no
(3) Open Eclipse, join the Tomcat server, join the way for example as seen.
You can use Tomcatserver after you've joined.
4. Invocation mode
<span style= "FONT-SIZE:14PX;" >import Javax.naming.context;import Javax.naming.initialcontext;import Javax.naming.namingexception;import Javax.sql.datasource;import java.sql.*;p Ublic class DBAccess {private static DataSource ds=null;private Context ctx= Null;private Connection Conn=null; Statement stat=null;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 ();} IF (conn! = null) { conn.close ();} } public int executeupdate (String sql) throws Sqlexception{int status=-1;try{conn=ds.getconnection (); Conn.setautocommit (false); stat =conn.createstatement (); status=stat.executeupdate (SQL); Conn.commit ();} Finally{daoclose ();} return status;}} </span>
The way to connect to a database is straightforward, using the Intialcontext function to read the contents of Context.xml. The database connection pool is then established. Instantiate a database connection where it is needed and use it.
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Linux environment Eclipse + Tomcat + MySQL architecture Java EE Method Development environment