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.
-
Are you specifying the connection in the code or using jndi?Kevin Bowersox– Kevin Bowersox2012-11-19 11:38:27 +00:00Commented Nov 19, 2012 at 11:38
-
I am giving connection using code...@kmb385rajG– rajG2012-11-19 11:41:19 +00:00Commented 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?ppeterka– ppeterka2012-11-19 11:48:05 +00:00Commented 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?rajG– rajG2012-11-19 11:54:53 +00:00Commented Nov 19, 2012 at 11:54
3 Answers
There are two main ways to obtain JDBC connections within a Java Web Application.
- Retrieve a connection from a DataSource registered in a JNDI directory service within the container.
- 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.
3 Comments
There are 2 scenarios here:
- 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. 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
in contex.xml using -- tag we can connect to any db