Reputation: 21
I am using the follwing class as my connection manager to make DB connectvity through datasource. The problem is that when I invoke this class through my DAOImpl class it is returning null.
private static DataSource dataSource;
private static Connection connection;
private ConnectionFactory() {
System.out.println(" ConnPoolFactory cons is called ");
}
public static synchronized Connection getConnection() throws SQLException {
try {
if (connection == null) {
Context ctx = new InitialContext();
Context envContext = (Context) ctx.lookup("java:/comp/env");
dataSource = (DataSource) envContext.lookup("jdbc/myoracle");
connection = dataSource.getConnection();
} else {
return connection;
}
} catch (NamingException e) {
e.printStackTrace();
}
System.out.println(connection);
return connection;
//System.out.println(connection);
}
DriverManager
for conectivity.DataSource
.But with the above code I get the following exception:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:
the following is my context.xml
<Resource auth="Container" driverClassName="oracle.jdbc.OracleDriver" name="jdbc/myoracle" password="password" type="javax.sql.DataSource" url="jdbc:oracle:thin:@10.49.116.42:1521:DBNAME" username="username"/>
Upvotes: 1
Views: 615
Reputation: 120
You mentioned about context.xml. So, assuming that you are using Apache Tomcat container for JDBC connectivity.
So, to me problem seems to be due below possibilities:
Note from Apache website: Tomcat specific resource configuration is entered in the elements that can be specified in either $CATALINA_BASE/conf/server.xml or, preferably, the per-web-application context XML file (META-INF/context.xml).
Note: If a resource has been defined in a element it is not necessary for that resource to be defined in /WEB-INF/web.xml. However, it is recommended to keep the entry in /WEB-INF/web.xml to document the resource requirements for the web application. So, you can try removing Oracle JDBC entry in web.xml and evaluate first if things work. Then put correct entry.
Upvotes: 1
Reputation: 419
I am really not able to understand what you are trying to ask...But if you want JDBC connectivity here is a way :
1.You have to Install JDBC driver for that RDBMS that you are using...if you are using any IDE then there is no need to install it separately...
2.you have to tell the computer where the Driver is stored
i>You have to set environment variables
After that you are ready to connect your java code with database
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:port_number/Database name","root","password");
you have to enter the Database name,port number and password for the database you are using NOTE : here database is MySql.For other RDBMS the url will be different. Hope this helps
Upvotes: 1
Reputation: 757
You write like this jdbc connectivity code
String url = "jdbc url" + "databaseName;userName;password;";
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(url);
}
catch (ClassNotFoundException cnfex)
{
cnfex.printStackTrace();
}
Upvotes: 1
Reputation: 332
sample code for connectivity
public static Connection Connect(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your Oracle JDBC Driver?");
e.printStackTrace();
return null;
}
System.out.println("Oracle JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection(
"YOUR JDBC URL", "USERNAME",
"PASSWORD");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return null;
}
return connection;
}
Upvotes: 1