java - Tomcat MySQL Connection DataSource not working -


i hard coding db connection in app, , right trying make better. issue seems that's supposed done, , still fails.

1- download binary distribution appropriate platform, extract jar file, , copy "$catalina_home/lib" - done.

2- configure mysql database jndi resource. resource in application's context elemen , resource reference in application's "web-inf/web.xml" file.

<resource name="jdbc/storagerdb"              auth="container"              type="javax.sql.datasource"              username="root"              password="admin"              driverclassname="com.mysql.jdbc.driver"              url="jdbc:mysql:/localhost:3306/storagerdb"              maxactive="15"              maxidle="3"/>   <resource-ref> <description>mysql datasource example</description> <res-ref-name>jdbc/storagerdb</res-ref-name> <res-type>javax.sql.datasource</res-type> <res-auth>container</res-auth> 

public class dataaccessobject {  private customlogger logger = new customlogger(dataaccessobject.class);  private statement stmt; private connection conn; private datasource ds; private context ctxt;  /**  * queries creates db.  */ private static final string[] db_creation_queries = new string[] {          "create database if not exists storagerdb;", "use storagerdb;",          "create table if not exists user(id int not null primary key auto_increment,"                 + "username varchar(25) not null, password varchar(250) not null,"                 + "salt varchar(250) not null, email varchar(35) not null);",          "create table if not exists header(id int not null primary key auto_increment,"                 + "user_id int, foreign key (user_id) references user(id)," + "server varchar(50) not null,"                 + "content_type varchar(50) not null," + "status_code int not null," + "url varchar(250) not null);"  };  /**  * constructor jdbc driver initialized , loaded in tomcat/lib  * @throws namingexception   */ public dataaccessobject() {     try {         ctxt = new initialcontext();         ds = (datasource)ctxt.lookup("java:comp/env/jdbc/storagerdb");         conn = ds.getconnection();          stmt = conn.createstatement();          (string query : db_creation_queries) {             stmt.execute(query);         }      }   catch (sqlexception | namingexception e) {         logger.logexceptionmessage("dao constructor sql", e.getstacktrace());     }  } 

it fails on "ds = (datasource)ctxt.lookup("java:comp/env/jdbc/storagerdb")".

exception in thread "main" javax.naming.noinitialcontextexception: need specify class name in environment or system property, or applet parameter, or in application resource file:  java.naming.factory.initial @ javax.naming.spi.namingmanager.getinitialcontext(unknown source) @ javax.naming.initialcontext.getdefaultinitctx(unknown source) @ javax.naming.initialcontext.geturlordefaultinitctx(unknown source) @ javax.naming.initialcontext.lookup(unknown source) @ com.wordpress.belichev.model.dataaccessobject.<init>(dataaccessobject.java:58) @ com.wordpress.belichev.model.dataaccessobject.main(dataaccessobject.java:279) 

the reason getting noinitialcontextexception running application standalone app. ignore web.xml file, resource-definition it. need run webapp give chance work.

getting npe in next line (as mentioned in comment) due app not having found datasource after context lookup. different problem, related web.xml usage. suggest taking @ post see main points.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -