MySql doesn't work from deployed java web app - java

I made a page using struts. When running on localhost (glassfish) it is working fine.
However, when I copy WAR file to a remote server and run the application from there login form does not work. That means something is wrong when connecting on mysql database.
How could I solve this?
Code snippet:
Connection conn = null;
Statement stmt = null;
try{
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/SB");
conn = ds.getConnection();
stmt = conn.createStatement();
ResultSet rs = null;
if(stmt.execute("SELECT * FROM users WHERE username='"+loginform.getUsername()+"' AND geslo=MD5('"+loginform.getPassword()+"') LIMIT 1;"))
{
rs = stmt.getResultSet();
}
rs.next();
Unfortunately I cannot read from the server console. What else should I do to see the exception (remote logging?)?
Thanks on replies.
UPDATE:
- Both locally and remotely GlassFish 3.0.1 is running.
- My working environment is NetBeans 6.9.1.
- Deploying locally is done simply in NetBeans. I just click on the project name and click publish. For remote deployment, admin gave me access to the FTP server where I just copy the WAR file.
- MySQL server is located somewhere else. The point is, it is accessible from the localhost web application but not from the same remote web application.

Assuming you have admin rights on the remote server, you need to
Verify that the datasource "jdbc/SB" exists. (Resources / JDBC / JDBC Resources)
Verify that the "poolname" in the datasoucre exists (Resources / JDBC/ Connection Pools)
Open the appropriate connection pool and click on the "ping" button.
If ping fails verify the connection params under the Additional Properties tab.
There could be a dozen other issues as well. You really need to get access to the server log so you see what the specific exception is. It's unlikely it's your code and more likely that the remote server is missing the driver dependency or isn't properly configured (the connection pool an datasource)

Some ideas are
- log to file instead of console
- Print log on your struts page
- maybe you don't have grants or firewall problem to access DB

I'd bet that you don't have authority to access the DB...
Try this as a test... install the MySQL client on another machine and then connect to the MySQL server from there.
If that fails then:
Say you log in with:
User: test
Password: password
Then open the Mysql client and enter:
use nameOfDatabase;
GRANT ALL ON nameofDatabase.* TO 'userName'#'ipaddressOfServerOrDNSName';
That should do it.
There are other ways to give access see http://dev.mysql.com/doc/refman/5.1/en/grant.html

Are you sure your database is setup correctly on your remote server? It sounds like maybe a firewall issue.

Unfortunately I cannot read from the server console.
If you have shell access to the remote server, the solution is simple:
Change the logging configs so that your webapp logs to a file instead of / as well as the console.
Login and read the file.
(Indeed, if you were using Tomcat, stuff written to System.out / System.err is written to a file anyway. Maybe this is true for Glassfish also.)
If you don't have shell access, you may have to modify your webapp to provide an extra page that can be used to download or view the log file from your web browser. If you have to resort to this, pay attention to the obvious security concerns.

This is my weg (wild eyed guess)....
Your local app server has the MySql driver installed. The JDBC resource is also defined correctly. The MySql server is probably running on your localhost. You have developed your app locally in NetBeans.
When you deploy an app from NetBeans onto a local server, the IDE makes sure that server has the DB driver installed. NetBeans cannot do that when you deploy to a remote host. If you use some other deployment method, driver installation is not automagic. It is an easy step to forget.
You can find out if the MySql drive is installed on your server by writing a web app that attempts to load or use one of the classes from the driver jar file. If the access generates an exception, you can probably get something that works by including the MySql drive in your webapp's WEB-INF/lib.
NetBeans also helps folks create and manage their JDBC resource definitions. If you create an app that targets a database on your localhost, the JDBC resource will have localhost as part of the URL. This will work great when the app is deployed onto the same server as the DB server. If the app is moved to another server, the JDBC resource needs to be changed to account for the possibility that the DB server is NOT on the localhost.
If you do not have admin access to the remote server, you will have trouble getting the resources defined. If your remote server was a GlassFish 3.1 domain/instance, you could leverage application scoped resources to get the resources defined for your app.
Regarding access to the log file.
If you are using asadmin to deploy onto the remote server, this command will help you access the server log if you are working with GlassFish v2.x: http://docs.sun.com/app/docs/doc/821-0179/display-log-records-1?l=en&a=view. You can also access the log via the admin console.
If you are using rcp to deploy your app, by copying it into the autodeploy directory of the remote server, you may want to see if you can copy the log onto your local machine.
For example... if your admin told you to deploy using an ftp command like this:
ftp> cd /a/b/glassfish/domains/domain1/autodeploy
ftp> put MyGreatWebapp.war
you may be able to get the log by doing something like this:
ftp> cd /a/b/glassfish/domains/domain1/logs
ftp> get server.log
Experiment....

Related

Missing Oracle Database connection Driver when running on an external host

When running the Java web service I'm building on Local Host, everything runs smoothly, however when I attempt to run the exact same code on a different host, such as on AWS, it fails.
I have narrowed the cause down to the fact that the database connection driver "oracle.jdbc.driver.OracleDriver" is absent when the application is not hosted locally, which means that the SQL queries are failing to execute for the obvious reason that they aren't being sent anywhere since the connection is failing to open.
How do I build my project such that the driver is included? Everything else should be fine as it runs as expected on localhost, however I can't run the web service locally, it isn't an option.
You should consider the way you are deploying your webservice. I will give you a checklist, so make sure everything is done.
Insert the oracle jdbc driver inside the libraries folder on your app -- you should consider java jvm version you are running, since your deployment environment may be different from your dev/testing environment;
2nd option: insert the oracle jdbc driver inside your container or application server libraries folder;
Add to your $CLASSPATH or %CLASSPATH% environment variable the right option for your oracle jdbc driver

Can't connect to an AWS MySQL RDS using JDBC when site is deployed to elastic beanstalk

So I created a MySQL DB on amazon running on the same account as my tomcat server, it built and is running correctly but can't connect to the database.
I can access the remote database running the site using tomcat on my computer, and I can access the db through MySQL workbench, but when I deploy and run it I get a 500 whenever the site tries to access the database.
I opened up my security on this database to accept any connection and port(lol), and I checked if the mysql java connector.jar in the WEB-INF/libs file is the latest version. I even tried setting the JDBC connection string to localhost to see if it would pick it up, but no luck.
I think I'm missing something, can anyone think of any reason my website's JDBC can access the remote database server from my machine but not from the amazon tomcat server?
Also, this is a student project I'm building.
Hope this description makes sense, maybe someone can think of something I haven't tried:)

How to create a batch file for connecting to Java DB Network Server

I have made a Netbeans application that is reliant on the DB Network Server in order to retrieve data. In Netbeans the code works fine and runs well. Outside of Netbeans everything but the database information is working. I have made a batch file for connecting to the localhost server that seems to connect on the port I assigned: 1527. Even after connecting to the localhost, it won't display the database information.
My code in the batch file:
PATH C:\Program Files\Java\jdk1.8.0_25\db\bin;%PATH%
startNetworkServer
When I run this I get the result:
Security manager installed using the Basic server security policy.
Apache Derby Network Server - 10.10.1.8 - (1557168) started and ready to accept connections on port 1527connect
Is there more code that I need to add in order to connect to the actual database itself from the server? Or is this not the right way to do this at all? I have tried using the Embedded DB but that didn't work and only caused more problems. I would prefer greatly to stay away from it and stick to the Network DB.

dotcms - won't see new database configuration

I'm in the midst of migrating a dotCMS installation to a new server. Everything seems to be working properly except for the database configuration. I've updated tomcat/conf/Catalina/localhost/ROOT.xml with a new IP address for our MySQL server, saved it, run ant deploy-plugins, and restarted dotcms/tomcat.
When I check the log, I get a number of SQL exceptions, all with the same error message: Host 'xxx' is not allowed to connect to this MySQL server. I double-checked all of my MySQL credentials, which were fine.
When I run netstat -pant, I can see an outgoing connection trying to reach the OLD MySQL server.
I've literally grepped the entire dotCMS folder hierarchy for this IP address (or hostname) and nothing turns up. Is there some kind of cache file I need to delete so the ROOT.xml changes are seen?
Sounds like you should check your MySQL permissions. You might need to GRANT access from the new IP address.
Are you sure there is no root.xml in the com.dotcms.config plugin that overwrites your root.xml after running ant deploy-plugins?

Running Apache Derby on a Flash Drive

I'm trying to run the Apache Derby db from a flash drive. I copied the relevant .jar files and managed to start up the network server. But how to I specify the connection URL in connecting to the server? the database is in the flash drive labeled as G.
Used the following code, but came across an exception:
DriverManager.getConnection("jdbc:derby://localhost:1527");
java.sql.SQLException: The URL 'jdbc:derby://localhost:1527' is not properly formed.
How can I connect and use it as a normal database?
Thank You!
The presence of the flash drive is immaterial to this question. The most pertinent point is whether the Derby server is running in the embedded mode or in the network server mode.
From the URL used, it appears that you intend to connect to Derby running as a network server. This would be the case if you've started Derby using the startNetworkServer shell scripts, available in the Derby installation. If so, the connection URL format, as defined in the Derby documentation is as shown below. Note the presence of the databaseName parameter, which is missing in the URL posted in the question.
jdbc:derby://server[:port]/databaseName[;attributeKey=value]..
If you didn't want to start Derby in the network server mode, but instead as an embedded database, then the connection URL format is different. Note the absence of the port number, and the reliance on a subprotocol whose value are one of directory, classpath or jar. Examples of this format can also be found in the documentation.
jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]*

Categories