url - how to configure connection to the database in clouds services -
i have surfed web 2 days looking better way configure application uploading appfog
connects mysql database in cloud.
i have created mysql service , did bind application. created tables , put data on them using local console ( using af tunnel mydatabase
) in linux
but apps not seem find database. application uses jdbc
... used database username
, password
, databasename
given me console (those funny chars in screen) , did not work. put own credentials, still, no success...
i tried using url
points application @ port given on console, still... actually, put in details showed on console connect database in cloud after deploying ... app seems not find database , tables....
i not know wrong...
please help..
more:
this code used try connection:
try { string connectionurl = "jdbc:mysql://http://someapp.aws.af.cm:10000 /onlinepassword"; class.forname("com.mysql.jdbc.driver"); conn = drivermanager.getconnection(connectionurl, user, pass); statement = conn.createstatement(); }
use appfog environment variable called vcap_services automatically detect mysql database credentials , connection info. can take advantage of display info in json format , use connect app mysql database. may try this.
first create servlet , retrieve database connection using context attribute follows:
public class dbinfoservlet extends httpservlet { public void doget(httpservletrequest request,httpservletresponse response) throws servletexception, ioexception { request.setattribute("jsoncontent", java.lang.system.getenv("vcap_services")); request.getrequestdispatcher("/index.jsp").view.forward(request, response); } }
then create index.jsp file display db connection info follows:
<body> <p> <!-- click here display db info --> <a href="/dbinfoservlet"></a> <!-- display of db connection , credentials info here. show in json document format --> <c:out value="${jsoncontent}" /> </p> </body>
use hostname(i.e. ip), port, name(i.e. database name), username, password parameters json document content modify jdbc connection code follows:
try { string connectionurl = "jdbc:mysql://hostname:3306/name"; class.forname("com.mysql.jdbc.driver"); conn = drivermanager.getconnection(connectionurl, username, password); statement = conn.createstatement(); }
if above doesn't work, ensure using latest version of mysql-connector-java jar file.
have fun!
Comments
Post a Comment