Connection with mysql with netbeans for jsp -


i using netbeans 7.0.1 ide jsp/servlet trying make database connection project. downloaded jar file 'mysql-connector-java-5.1.24-bin.jar' pasted jdk's jre/lib dir, added netbean projects libraries dir. created servlet , wrote following code:

import java.sql.*; import java.io.ioexception; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse;   public class tstjdbc extends httpservlet { protected void doget(httpservletrequest request, httpservletresponse response)         throws servletexception, ioexception {      try{     string dburl = "jdbc:mysql://localhost:3306/murach";           string username="root";          string password="1234";           connection con2 = drivermanager.getconnection(dburl, username, password);           string query = "insert tbluser1(firstname) values('shaon')";            statement statmnt = con2.createstatement();           statmnt.executeupdate(query);     }      catch(sqlexception e)     {         e.printstacktrace();     } } 

}

but can establish connection. line connection con2, directly going catch() block ; without executing query.

try loading driver prior using drivermanager class.

try{      string dburl = "jdbc:mysql://localhost:3306/murach";       string username="root";      string password="1234";        class.forname("com.mysql.jdbc.driver");//load driver       connection con2 = drivermanager.getconnection(dburl, username, password);      string query = "insert tbluser1(firstname) values('shaon')";       statement statmnt = con2.createstatement();      statmnt.executeupdate(query); } 

from o'reilly:

before can use driver, must registered jdbc drivermanager. typically done loading driver class using class.forname( ) method:

this required since have placed library within jdk/lib folder i'm assuming loaded using different classloader 1 used application. since different class loaders used automatic registration takes place jdbc 4.0+ drivers not take effect. try place driver jar file within lib of application server, should use same classloader of application. see: when class.forname needed when connecting database via jdbc in web app?

regarding automatic registration

in jdbc 4.0, no longer need explicitly load jdbc drivers using class.forname(). when method getconnection called, drivermanager attempt locate suitable driver among jdbc drivers loaded @ initialization , loaded explicitly using same class loader current application.

the drivermanager methods getconnection , getdrivers have been enhanced support java se service provider mechanism (spm). according spm, service defined well-known set of interfaces , abstract classes, , service provider specific implementation of service. specifies service provider configuration files stored in meta-inf/services directory. jdbc 4.0 drivers must include file meta-inf/services/java.sql.driver. file contains name of jdbc driver's implementation of java.sql.driver. example, load jdbc driver connect apache derby database, meta-inf/services/java.sql.driver file contain following entry:

org.apache.derby.jdbc.embeddeddriver

let's take quick @ how can use new feature load jdbc driver manager. following listing shows sample code typically use load jdbc driver. let's assume need connect apache derby database, since using in sample application explained later in article:

class.forname("org.apache.derby.jdbc.embeddeddriver"); connection conn =     drivermanager.getconnection(jdbcurl, jdbcuser, jdbcpassword); 

but in jdbc 4.0, don't need class.forname() line. can call getconnection() database connection.

source

regarding service loaders

for purpose of loading, service represented single type, is, single interface or abstract class. (a concrete class can used, not recommended.) provider of given service contains 1 or more concrete classes extend service type data , code specific provider. provider class typically not entire provider rather proxy contains enough information decide whether provider able satisfy particular request code can create actual provider on demand. details of provider classes tend highly service-specific; no single class or interface possibly unify them, no such type defined here. requirement enforced facility provider classes must have zero-argument constructor can instantiated during loading.

a service provider identified placing provider-configuration file in resource directory meta-inf/services. file's name fully-qualified binary name of service's type. file contains list of fully-qualified binary names of concrete provider classes, 1 per line. space , tab characters surrounding each name, blank lines, ignored. comment character '#' ('\u0023', number sign); on each line characters following first comment character ignored. file must encoded in utf-8.

if particular concrete provider class named in more 1 configuration file, or named in same configuration file more once, duplicates ignored. configuration file naming particular provider need not in same jar file or other distribution unit provider itself. provider must accessible same class loader queried locate configuration file; note not class loader file loaded.

source


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -