3

I have created java web application in eclipse with database mysql.But now i have to deploy that web application in tomcat server.I know how to deploy web application without databas but need assistance with database. Thank you in advance.

4
  • Are you specifying the connection in the code or using jndi? Commented Nov 19, 2012 at 11:38
  • I am giving connection using code...@kmb385 Commented Nov 19, 2012 at 11:41
  • It is absolutely unclear what you want to ask. Please clarify: 1.) do you have the MySQL DB set up and deployed for the live environment? 2.) Do you have the right content (tables, rows, etc.) in the DB? 3.) Can you connect to it? 4.) Can you access the data? Which step do you have problem with? Commented Nov 19, 2012 at 11:48
  • 1)yes,i have mysql db setup 2)yes i have all right contents like table etc in db 3)yes i can connect to it through my web application an insert ,update and delete tha db data 4)i can access th data.Now i have prblem with final step when i creating .war file for deployment how to mysql db file we can add to it? Commented Nov 19, 2012 at 11:54

3 Answers 3

3

There are two main ways to obtain JDBC connections within a Java Web Application.

  1. Retrieve a connection from a DataSource registered in a JNDI directory service within the container.
  2. Creating a Connection manually within your application code.

JNDI

Using JNDI requires a connection pool to be created within tomcat. This can be done within the context.xml file in tomcat's config directory.

Example Context.xml Entry

  <Resource name="jdbc/EmployeeDB"
            auth="Container"
            type="javax.sql.DataSource"
            username="dbusername"
            password="dbpassword"
            driverClassName="org.hsql.jdbcDriver"
            url="jdbc:HypersonicSQL:database"
            maxActive="8"
            maxIdle="4"/>

This connection would then be retrieved in your code as follows:

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
  envCtx.lookup("jdbc/EmployeeDB");

Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();

Manual Creation

Manually creating the connection within your code is simpler, however JNDI is recommended for its portability.

Manual Example

public class MysqlConnect{
   public static void main(String[] args) {

      Connection conn = null;
      String url = "jdbc:mysql://localhost:3306/";
      String dbName = "jdbctutorial";
      String driver = "com.mysql.jdbc.Driver";
      String userName = "root"; 
      String password = "root";

      try {
          Class.forName(driver).newInstance();
          conn = DriverManager.getConnection(url+dbName,userName,password);
          conn.close();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
}

When deploying for either of these scenarios it is important that you have the appropriate JDBC driver in your classpath.

Sign up to request clarification or add additional context in comments.

3 Comments

i used the 2nd option to connect to mysql db,so can tell me how to deploy my web application?
There is really nothing special about deploying, just make sure you have the driver in your classpath. Do you get a specific error?
i want to make my mysql db file portable to add it in .war file just like mssql server .mdf file
1

There are 2 scenarios here:

  1. You define the connection directly in the code using simple JDBC and Class.forName() - in that case you just need to make sure the jar containing the driver is in the classpath and it should work.
  2. This is the preferred method - Define a Datasource on the server and call it in the code by using the JNDI API:

    InitialContext ic = new InitialContext();  
    DataSource ds = (DataSource)ic.lookup("jdbc/testDS");  
    conn = ds.getConnection();  
    

Comments

0

in contex.xml using -- tag we can connect to any db

3 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.
<Resource name="jdbc/yourchoicename" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="root" password="root" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/dbname?autoReconnect=true"/>
i think it is perfect for any db that may be local or remote

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.