android - Why i can't connect to mysql database in java? -


i found code in tutorial isn't working when debug try connection , isn't throw exception go block without line of code under drivermanager.getconnection(). why? has idea?

            connection con = null;             statement st = null;             resultset rs = null;              string url = "jdbc:mysql://host/databasename";             string user = "user";             string password = "pass";               try {                 class.forname("com.mysql.jdbc.driver");             } catch (classnotfoundexception e1) {                 // todo auto-generated catch block                 e1.printstacktrace();             }              try {                 con = (connection) drivermanager.getconnection(url, user, password);                 st = (statement) con.createstatement();                 rs = st.executequery("select * message");                  if (rs.next()) {                     system.out.println(rs.getstring(1));                 }              } catch (sqlexception ex) {                 logger lgr = logger.getlogger(version.class.getname());                 lgr.log(level.severe, ex.getmessage(), ex);              } catch (java.sql.sqlexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }              catch (exception e) {                 // todo: handle exception                 e.printstacktrace();             }             {                 try {                     if (rs != null) {                         rs.close();                     }                     if (st != null) {                         st.close();                     }                     if (con != null) {                         con.close();                     }                  } catch (sqlexception ex) {                     logger lgr = logger.getlogger(version.class.getname());                     lgr.log(level.warning, ex.getmessage(), ex);                 } catch (java.sql.sqlexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 }             } 

logcat write this: logcat message

1) drivermanager.getconnection() returns object of type connection, there no need cast it. same thing st = (statement) con.createstatement();

2) unless you're running mysql on remote machine, need make sure have mysql installed on local machine. if decide run mysql on local machine, can connect string url = "jdbc:mysql://localhost/{existing_db_name}"; given else remains same.

3) seems you're catching same exception twice:

try{     con = (connection) drivermanager.getconnection(url, user, password);     ... }catch (sqlexception ex) {     logger lgr = logger.getlogger(version.class.getname());     lgr.log(level.severe, ex.getmessage(), ex); } catch (java.sql.sqlexception e) {     e.printstacktrace(); } 

as result, second catch block, 1 prints exception message system.err, never runs. instead, print exception message logger. might why think no exception being thrown, when in reality, exception is being thrown.

4) make sure download jdbc driver mysql. copy , paste directory of project.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -