mysql - connect to database with JSP -
please want connect database, have added mysql connector eclipse can't connect base me please
<% try { string connectionurl = "jdbc:mysql://localhost:3306/grp_asso"; connection connection = null; class.forname("com.mysql.jdbc.driver").newinstance(); connection = drivermanager.getconnection(connectionurl,"root",""); if(!connection.isclosed()) out.println("successfully connected"); connection.close();}catch(exception ex){ out.println("unable connect database");} %>
here java class wrote connect database. need add database servlet container xml configuration files - web-inf/web.xml , meta-inf/context.xml if you're using tomcat (which jboss, glassfish , spring use, in case you're using of those.)
package your_package; import java.sql.connection; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import javax.naming.namingexception; public class connectionpool { private connection connection = null; private javax.sql.datasource datasource = null; private javax.naming.context envcontext = null; private javax.naming.context initcontext = null; private string envcontextdatasource = "jdbc/datasourcename"; private string envconextlookup = "java:/comp/env"; public connectionpool() { super(); } private void lookupdatasource() throws namingexception { this.initcontext = new javax.naming.initialcontext(); this.envcontext = (javax.naming.context)this.initcontext.lookup(this.envconextlookup); this.datasource = (javax.sql.datasource)this.envcontext.lookup(this.envcontextdatasource); } public void openconnection() throws sqlexception, namingexception { if (this.datasource == null) { lookupdatasource(); } this.connection = datasource.getconnection(); } public void closeconnection() { try { if (this.connection != null) { this.connection.close(); this.connection = null; } } catch (sqlexception e) { this.connection = null; } } public connection getconnection() { return connection; } public void setconnection(connection connection) { this.connection = connection; } public javax.sql.datasource getdatasource() { return this.datasource; } public void setdatasource(javax.sql.datasource datasource) { this.datasource = datasource; } public javax.naming.context getenvcontext() { return this.envcontext; } public void setenvcontext(javax.naming.context envcontext) { this.envcontext = envcontext; } public javax.naming.context getinitcontext() { return this.initcontext; } public void setinitcontext(javax.naming.context initcontext) { this.initcontext = initcontext; } public string getenvcontextdatasource() { return envcontextdatasource; } public void setenvcontextdatasource(string envcontextdatasource) { this.envcontextdatasource = envcontextdatasource; } public string getenvconextlookup() { return envconextlookup; } public void setenvconextlookup(string envconextlookup) { this.envconextlookup = envconextlookup; } } in web-inf/web.xml need reference data source:
<resource-ref> <description>datasourcename</description> <res-ref-name>jdbc/datasourcename</res-ref-name> <res-type>javax.sql.datasource</res-type> <res-auth>container</res-auth> </resource-ref> in meta-inf/context.xml need link data source database.
change "name", "username", "password", , "url" tags correct values configuration - have provided sample values. if you're testing username root , password blank.
<?xml version="1.0" encoding="utf-8"?> <context> <resource name="jdbc/datasourcename" auth="container" type="javax.sql.datasource" username="username" password="password" driverclassname="com.mysql.jdbc.driver" url="jdbc:mysql://localhost:3306/database_name?autoreconnect=true" validationquery="select 1" maxactive="1000" maxidle="10"/> </context>
Comments
Post a Comment