My app seems to be hanging overnight because of the connection getting dropped(I think that's the problem.) How can I structure my app so that it can try to roll up a new connection?
Since the incident I have updated the getConnection() method that my app uses as so:
private Connection getConnection() {
boolean failed = false;
try{
failed = !connection.isValid(1000);
}catch(SQLException ex){
System.out.println("WARNING: Connection not valid!");
}
try{
failed = connection.isClosed();
}catch(SQLException ex){
System.out.println("WARNING: Connection is closed!");
}
if(failed){
System.out.println("Renewing connection");
this.initializeConnection();
}
return connection;
}
This is the output:
** BEGIN NESTED EXCEPTION **
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
MESSAGE: The last packet successfully received from the server was54521 seconds
ago.The last packet sent successfully to the server was 54521 seconds ago, which
is longer than the server configured value of 'wait_timeout'. You should consi
der either expiring and/or testing connection validity before use in your applic
ation, increasing the server configured values for client timeouts, or using the
Connector/J connection property 'autoReconnect=true' to avoid this problem.
STACKTRACE:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet success
fully received from the server was54521 seconds ago.The last packet sent success
fully to the server was 54521 seconds ago, which is longer than the server conf
igured value of 'wait_timeout'. You should consider either expiring and/or testi
ng connection validity before use in your application, increasing the server con
figured values for client timeouts, or using the Connector/J connection property
'autoReconnect=true' to avoid this problem.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
rce)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1
074)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3246)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1917)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2536)
at com.mysql.jdbc.ConnectionImpl.setCatalog(ConnectionImpl.java:4962)
at com.mysql.jdbc.DatabaseMetaData.getCallStmtParameterTypes(DatabaseMet
aData.java:1506)
at com.mysql.jdbc.DatabaseMetaData.getProcedureOrFunctionColumns(Databas
eMetaData.java:4120)
at com.mysql.jdbc.DatabaseMetaData.getProcedureColumns(DatabaseMetaData.
java:4057)
at com.mysql.jdbc.CallableStatement.determineParameterTypes(CallableStat
ement.java:809)
at com.mysql.jdbc.CallableStatement.<init>(CallableStatement.java:609)
at com.mysql.jdbc.JDBC4CallableStatement.<init>(JDBC4CallableStatement.j
ava:46)
at sun.reflect.GeneratedConstructorAccessor6.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
rce)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.CallableStatement.getInstance(CallableStatement.java:5
05)
at com.mysql.jdbc.ConnectionImpl.parseCallableStatement(ConnectionImpl.j
ava:3881)
at com.mysql.jdbc.ConnectionImpl.prepareCall(ConnectionImpl.java:3965)
at com.mysql.jdbc.ConnectionImpl.prepareCall(ConnectionImpl.java:3939)
at com.protocase.hmiclient.db.HMIDatabaseAdapter.getAvailableBatchesForW
orkstation(HMIDatabaseAdapter.java:471)
at com.protocase.hmiclient.views.WorkstationContainer.getBatches(Worksta
tionContainer.java:74)
at com.protocase.hmiclient.views.BatchList.<init>(BatchList.java:55)
at com.protocase.hmiclient.views.WorkstationContainer.goToBatchList(Work
stationContainer.java:56)
at com.protocase.hmiclient.views.forms.BatchListControlPanel.refreshButt
onActionPerformed(BatchListControlPanel.java:118)
at com.protocase.hmiclient.views.forms.BatchListControlPanel.access$200(
BatchListControlPanel.java:16)
at com.protocase.hmiclient.views.forms.BatchListControlPanel$3.actionPer
formed(BatchListControlPanel.java:64)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.net.SocketException: Software caused connection abort: socket wr
ite error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3227)
... 61 more
** END NESTED EXCEPTION **
This exception suggests that you're opening the connection only once during application's startup and keeping forever open during the application's lifetime. This is bad. The DB will reclaim the connection sooner or later because it's been open for too long. You should close connections properly in the finally block of the very same try block as you're opening it and executing the query on it.
E.g.
public Entity find(Long id) throws SQLException {
Connection connection = null;
// ...
try {
connection = database.getConnection();
// ...
} finally {
// ...
if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
}
return entity;
}
If you have a performance concern regarding this (which is very reasonable as connecting is the most expensive task), then you should be using a connection pool. It also transparently handles this kind of "connection dropped" problems. For example, BoneCP. Please note that also in case of a connection pool, you should still be closing the connections in the finally block as per the above JDBC code idiom. It will namely make them available for reuse.
Related
I am currently trying to access mySQL-database through a Java Application.
In order to do so, I downloaded the "mysql-connector-java-5.1.42-bin.jar" file and imported it to Eclipse Neon.3.
My MySQL Server is up and running as following screenshot Shows:
I have tried it with following code:
final private String user = "root";
final private String passwd = "root";
public void connectToDB() throws Exception {
try {
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
String url= "jdbc:mysql://xxx:3306/pilotproject";
// Setup the connection with the DB
connect = DriverManager.getConnection(url, user, passwd);
System.out.println("Database connection established");
{
{
but I am getting following error:
java.sql.SQLException: null, message from server: "Host 'xxx.fuchs.com' is not allowed to connect to this MySQL server"
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1040)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2194)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2225)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2024)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:779)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at MySQLAccess.connectToDB(MySQLAccess.java:29)
at PilotFrame1$1.run(PilotFrame1.java:44)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Any suggestions on how I can solve this Problem?
I appreciate every help or advice you can give me, thanks a lot in advance!
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
FLUSH PRIVILEGES;
this helped!
I have this Java application in which I am now developing a module that can read and execute SQL statements from a file and output the results of the queries. If I launch the module via its main method, it works fine. I give the path to the DB connection settings file (with the user, pass, port.. etc), the path to a file containing SQL queries and the name of the output file. As I said, launched as stand alone, it works fine, connects with no problem, executes queries and outputs their results.
If I integrate it in the application workflow, it fails to connect to the database, even if the credentials (i.e., DB connection settings file) are the same. I have tried to wrap it around in a thread, tried to search if I need to launch the connection asynchronously, but nothing helped. The mysql-connector-java-5.1.28 is in the classpath, everything looks fine. I have also tried to set the autoReconnect (the commented line) but it also fails. This is the code to create the connection:
String url = "jdbc:mysql://"+server+":3306/?useCursorFetch=true";
//String url = "jdbc:mysql://"+server+":3306/?autoReconnect=true";
try {
return DriverManager.getConnection(url, user, password);
} catch (SQLException e1) {
Logging.outputStackTrace(e1);
throw new RuntimeException("Cannot connect to DB server: " + e1.getMessage());
with server = localhost. The user and pass are fine, sorry for repeating a third time, the connection is successful while launching it via the module's main method.
This is the stack trace I get while running the module via the application:
ERROR: An error occurred : Could not create connection to database server. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
Check the error log for details
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
java.lang.reflect.Constructor.newInstance(Unknown Source)
com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
com.mysql.jdbc.Util.getInstance(Util.java:386)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2575)
com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2311)
com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
java.lang.reflect.Constructor.newInstance(Unknown Source)
com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:347)
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
org.application.utilities.DBConnection.connectToMySQL(DBConnection.java:144)
org.application.utilities.DBConnection.connect(DBConnection.java:107)
org.application.utilities.RunSQL.run(RunSQL.java:110)
org.application.workflow.Processing.extractData(Processing.java:201)
org.application.workflow.Processing.<init>(Processing.java:73)
org.application.gui.ApiGUI$2.actionPerformed(ApiGUI.java:349)
javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
javax.swing.DefaultButtonModel.setPressed(Unknown Source)
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
java.awt.Component.processMouseEvent(Unknown Source)
javax.swing.JComponent.processMouseEvent(Unknown Source)
java.awt.Component.processEvent(Unknown Source)
java.awt.Container.processEvent(Unknown Source)
java.awt.Component.dispatchEventImpl(Unknown Source)
java.awt.Container.dispatchEventImpl(Unknown Source)
java.awt.Component.dispatchEvent(Unknown Source)
java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
java.awt.Container.dispatchEventImpl(Unknown Source)
java.awt.Window.dispatchEventImpl(Unknown Source)
java.awt.Component.dispatchEvent(Unknown Source)
java.awt.EventQueue.dispatchEventImpl(Unknown Source)
java.awt.EventQueue.access$200(Unknown Source)
java.awt.EventQueue$3.run(Unknown Source)
java.awt.EventQueue$3.run(Unknown Source)
java.security.AccessController.doPrivileged(Native Method)
java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
java.awt.EventQueue$4.run(Unknown Source)
java.awt.EventQueue$4.run(Unknown Source)
java.security.AccessController.doPrivileged(Native Method)
java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
java.awt.EventQueue.dispatchEvent(Unknown Source)
java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
java.awt.EventDispatchThread.pumpEvents(Unknown Source)
java.awt.EventDispatchThread.pumpEvents(Unknown Source)
java.awt.EventDispatchThread.run(Unknown Source)
Any ideas why this is happening? Is this a thread related issue? Can the event manager of the GUI have something to do with it? There are no multiple connections or concurrent connections to the database. Only once and it is properly closed when done. Via MySQL command line I cal also access my database, I made sure the server is on everytime, etc...
Thank you.
i think you forgot to mention database name
String url = "jdbc:mysql://"+server+":3306/?useCursorFetch=true";
must be
String url = "jdbc:mysql://"+server+":3306/DB_NAME?useCursorFetch=true";
OK. Got it. When I was running the SQL module via the application, I was setting the file encoding to ANSI via
System.setProperty("file.encoding", "ANSI");
which was not the case while running it stand alone.
Apparently the DriverManager.getConnection(url, user, pass) does not set the character set on its own to make sure it sends requests in a compatible character encoding. Maybe that should be fixed in the java.sql package.
I am trying to connect to FTP Server using Apache FTPCleint in java web start aplication.
Below is the code i am using.
ftpInstance = new FTPClient();
ftpInstance.setRemoteVerificationEnabled(false);
ftpInstance.connect(<HostName>);
int rpyCode = ftpInstance.getReplyCode();
if(!FTPReply.isPositiveCompletion(rpyCode)){
throw new Exception("Connection Rejected with reply code " + rpyCode + ". - " + <Hostname>);
}
Problem is at line ftpInstance.connect(<HostName>). An exception is been thrown Please look at the stack trace
java.lang.RuntimeException: java.net.SocketException: Malformed reply from SOCKS server
at com.newgen.backend.Operation.call(Operation.java:91)
at filetransfermanager.FileTransferManagerView$1.windowOpened(FileTransferManagerView.java:48)
at java.awt.AWTEventMulticaster.windowOpened(Unknown Source)
at java.awt.Window.processWindowEvent(Unknown Source)
at javax.swing.JFrame.processWindowEvent(Unknown Source)
at java.awt.Window.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.net.SocketException: Malformed reply from SOCKS server
at java.net.SocksSocketImpl.readSocksReply(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:171)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:192)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:285)
at com.newgen.ftp.FTPInstance.getInstance(FTPInstance.java:38)
at com.newgen.backend.Transport.<init>(Transport.java:58)
at com.newgen.backend.Operation.call(Operation.java:81)
... 27 more
Above code is working fine in normal project but not in java web start.
Any help is appreciated.
Thanks.
You have configured SOCKS proxy settings that point to something that isn't a SOCKS proxy.
I am making an applet that connects to a database that is stored on the same web server, and when I test the code in the applet viewer in Eclipse, it connects. Problem is, when I try to run it outside Eclipse (on web server or locally), I get this error:
Exception in thread "AWT-EventQueue-2" java.lang.ExceptionInInitializerError
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:286)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.j_ctrl.MovePanel.connectDB(MovePanel.java:569)
at com.j_ctrl.MovePanel.showHighScore(MovePanel.java:558)
at com.j_ctrl.MovePanel.enterPress(MovePanel.java:544)
at com.j_ctrl.MovePanel$1.keyPressed(MovePanel.java:163)
at java.awt.Component.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.security.AccessControlException: access denied (java.util.PropertyPermission file.encoding read)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPropertyAccess(Unknown Source)
at java.lang.System.getProperty(Unknown Source)
at com.mysql.jdbc.StringUtils.<clinit>(StringUtils.java:70)
... 39 more
Is there anything special that needs to be done in order to connect to an SQL database from an applet?
Here is the code that handles the connection:
private void connectDB(){
try{
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName);
String url = "jdbc:mysql://localhost/database";
String user = "user";
String pass = "pass";
connection = DriverManager.getConnection(url, user, pass);
System.out.println("Connected");
}catch(Exception ex){
ex.printStackTrace();
}
}
I changed the username, password and database here, but in my code they are correct.
Edit
Since connecting to the SQL server didn't work (I imagine it was hosted on a different host), I have ended up changing the way I access the database. The applet opens a connection to a PHP file on the host, which in turn connects to the database. Either way this is more secure.
This is being caused by the security model for applets. Here's a good article summarizing the two main ways around this issue - either sign the applet or use a policy file. Let us know if you still experience problems after trying one of these:
http://www.coderanch.com/how-to/java/HowCanAnAppletReadFilesOnTheLocalFileSystem
You need to sign your applet. Here is some good documentation on how.
we're programming a software for economic calculation. We use Java 6, the Swing Application Framework, MySQL 5.1, JDBC and Hibernate (JDBC is just for an initial connection-test).
We deployed that application on a Windows XP machine. As long as a user with administrator-privileges is running the application, it works just fine. If a user with restricted rights is trying to run it, we get an exception.
We tried to deactivate windows firewall and faced the same problem (administrator has firewall activated too!). Next we tried to reconstruct the situation at one of our computers and were not able to do so, the application runs just fine.
Our research in the internet wasn't successful, because in this case it is a very rare situation. Below is the code where the exception gets thrown and the stacktrace.
/**
* Testet ob eine Verbindung zu der Datenbank mit den Werten aus der Properties-Datei
* möglich ist.
* #return true, falls die Verbindung zustande kommt
*/
public boolean testConnection(Properties props) {
Connection conn = null;
try {
Class.forName(props.getProperty(SystemParameterModel.HIBERNATE_CONNECTION_DRIVER_CLASS_KEY)).newInstance();
System.out.println("Driver: " + props.getProperty(HIBERNATE_CONNECTION_DRIVER_CLASS_KEY));
System.out.println("URL: " + props.getProperty(HIBERNATE_CONNECTION_URL));
System.out.println("User: " + props.getProperty(HIBERNATE_CONNECTION_USER_KEY));
System.out.println("Pass: " + props.getProperty(HIBERNATE_CONNECTION_PASSWORD_KEY));
conn = DriverManager.getConnection(
props.getProperty(HIBERNATE_CONNECTION_URL),
props.getProperty(HIBERNATE_CONNECTION_USER_KEY),
props.getProperty(HIBERNATE_CONNECTION_PASSWORD_KEY));
} catch (SQLException ex) {
System.out.println(ex.getLocalizedMessage());
ex.printStackTrace();
return false;
} catch (Exception ex) {
System.out.println(ex.getLocalizedMessage());
ex.printStackTrace();
return false;
}
try {
conn.close();
} catch (SQLException ex) {
System.out.println("whateves..." + ex.getMessage());
}
return true;
}
And the Stacktrace:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1118)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:343)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2308)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2122)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:774)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:375)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:289)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.sgp.mybe.calc.model.SystemParameterModel.testConnection(SystemParameterModel.java:153)
at com.sgp.mybe.calc.dao.InitEntityManagerFactory.loadEMF(InitEntityManagerFactory.java:61)
at com.sgp.mybe.calc.dao.InitEntityManagerFactory.<clinit>(InitEntityManagerFactory.java:31)
at com.sgp.mybe.calc.dao.DaoFactory.getAttributeDao(DaoFactory.java:18)
at com.sgp.mybe.calc.model.ValidationAttributeModel.<init>(ValidationAttributeModel.java:24)
at com.sgp.mybe.calc.model.CalcDetailModel.<init>(CalcDetailModel.java:36)
at com.sgp.mybe.calc.main.MybeCalcApplication.startup(MybeCalcApplication.java:46)
at org.jdesktop.application.Application$1.run(Application.java:171)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.net.SocketException: Malformed reply from SOCKS server
at java.net.SocksSocketImpl.readSocksReply(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:292)
... 29 more
The properties are right, there's no doubt.
I hope someone can help me out, because this problem is driving me mad. Thank you!
you can refer this: https://serverfault.com/questions/89955/unable-to-connect-to-mysql-through-jdbc-connector-through-tomcat-or-externally
The problem is solved. The customer missinformed us about the proxy-settings in the local network, which produced the exception.