Servlets - DB connect

Hello All,

I am trying to insert a data in db when calling the servlet, it is working fine when in the code i use the connection string of mysql.
But it is not connecting to db, when using the datasource defined in the admin console eg: jdbc/test
Please let me know if the below code used is correct:

connection string
private Connection getConnection() {
Connection connection = null;
try {
Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/test");
connection = dataSource.getConnection();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}

Regards,
Sandeep

OriginalPostID-253947

  Discussion posts and replies are publicly visible