简体   繁体   中英

How to make JDBC connectivity

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);

} 
  • I am able to make this class work if I use DriverManager for conectivity.
  • I am able to achieve JDBC connectvity through servlet class using a 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"/>

我认为您错过了用于数据库连接的主机和端口

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;

}

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();
        }

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

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:

  1. If two resources with the same name both declared in the context.xml files under the web application's META-INF directory and in the $CATALINA_BASE/conf directory, then the internal version takes precedence. Please check if you have context.xml under META-INF and you have done correct config in CATALINA_BASE/conf server.xml. If so, update context.xml file under META-INF.

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).

  1. Note that the resource name (as mentioned in your code, jdbc/myoracle) must match the value specified in the web application deployment descriptor. Link explains how to: https://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html

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.

  1. If you like to test JDBC connectivity without using container, you could use RMI registry. Post below has example for MYSQL which you can modify for Oracle to test: javax.naming.NoInitialContextException with mysql DataSource

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM