LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [ JSP/JAVA ] MySQL connection fails (https://www.linuxquestions.org/questions/programming-9/%5B-jsp-java-%5D-mysql-connection-fails-218903/)

White R4bbit 08-17-2004 03:35 PM

[ JSP/JAVA ] MySQL connection fails
 
Hi all,
I've got this problem with JSP and MySQL: when I try to load my jsp page which should connect to the database, I get this error
Code:

java.sql.SQLException:
Unable to connect to any hosts due to exception: java.security.AccessControlException:
access denied (java.net.SocketPermission localhost resolve)

If I try accessing the database from Java, with a similar code, it runs correctly.
I have no firewalls blocking connections and I cannot understand why it does work with Java but not with JSP.
I'm using Tomcat as webserver and all the jsp pages that don't use a database run correctly.

Any ideas?

Thank you very much :study:

Jose Muņiz 08-17-2004 07:34 PM

Maybe you can show us your code in charge of creating the connection? Are you using the correct username / password combination? Are you sure you have correctly set up the access rights in your database, considering your location?

White R4bbit 08-18-2004 04:02 AM

Okay I attach both the Java and the JSP code; I'm not sure I've configured everything correctly, but I have a user "root" with no password and with the Java code it all works perfectly. Are there some other aspects of mysql configuration that I should have checked in order to make it work with tomcat/jsp?
Thank you.

Java code (working):
Code:

import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;

public class Provadb extends JFrame {
       
        static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        static final String DATABASE_URL="jdbc:mysql://localhost:3306/JSPProva";
       
        private Connection connection;
        private Statement statement;
       
        // costruttore
        public Provadb()
        {
                super("Tabella Persone del database JSPProva");
                try{
                        //System.setProperty("db2j.system.home", "/usr/share/mysql/");
                        Class.forName( JDBC_DRIVER );
                        connection = DriverManager.getConnection( DATABASE_URL, "root", "" );
                        statement = connection.createStatement();
                       
                        ResultSet resultSet = statement.executeQuery( "SELECT * FROM Persone" );
                       
                        StringBuffer results = new StringBuffer();
                        ResultSetMetaData metaData = resultSet.getMetaData();
                        int numberOfColumns = metaData.getColumnCount();
                       
                        for(int i=1; i<=numberOfColumns; i++)
                                results.append( metaData.getColumnName( i ) + "\t" );
                       
                        results.append("\n");
                       
                        while( resultSet.next() ){
                                for(int i=1; i<=numberOfColumns; i++)
                                        results.append( resultSet.getObject( i ) + "\t" );
                               
                                results.append("\n");
                        }
                       
                        // crea l'interfaccia grafica
                        JTextArea textArea = new JTextArea( results.toString() );
                        Container container = getContentPane();
                       
                        container.add( new JScrollPane( textArea ) );
                       
                        setSize( 500,200 );
                        setVisible( true );
                }
               
                catch( SQLException sqlException ){
                        JOptionPane.showMessageDialog( null, sqlException.getMessage(), "Errore nel database...", JOptionPane.ERROR_MESSAGE );
                        System.exit( 1 );
                }
               
                catch( ClassNotFoundException classNotFound ){
                        JOptionPane.showMessageDialog( null, classNotFound.getMessage(), "Impossibile reperire il driver...", JOptionPane.ERROR_MESSAGE );
                        System.exit( 1 );
                }
               
                finally{
                        try{
                                statement.close();
                                connection.close();
                        }
                       
                        catch( SQLException sqlException ){
                                JOptionPane.showMessageDialog( null, sqlException.getMessage(), "Errore nel database (finally)...", JOptionPane.ERROR_MESSAGE );
                                System.exit( 1 );
                        }
                       
                }
               
        } // fine del costruttore
       
        public static void main( String args[] )
        {       
                Provadb window = new Provadb();
                window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        }
       
}

JSP code (not working):
Code:

<%@ page import="java.sql.*" %>
<%! String DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost:3306/JSPProva";
%>

<html>

<head>
<title>Connessione a DB attraverso JDBC</title>
</head>

<body>
  <h1>Accesso ad un database MySQL</h1>
 
  <%                                               
  Class.forName(DRIVER);                  // Carico il driver.
  Connection connection = null;        // Preparo il riferimento alla connessione.
  try {                                               
  connection = DriverManager.getConnection(DB_URL, "root", "");                // Apro la connesione verso il database. 
  Statement statement = connection.createStatement();        // Ottengo lo Statement per interagire con il database.
  ResultSet resultset = statement.executeQuery(                        // Interrogo il DBMS mediante una query SQL.
  "SELECT Nome, Cognome, Indirizzo FROM Persone");                // Scorro e mostro i risultati.
  while (resultset.next()) {
  String nome = resultset.getString(1);
  String cognome = resultset.getString(2);
  String indirizzo = resultset.getString(3); %>
  <b>Nome:</b> <%= nome %><br>
  <b>Cognome:</b> <%= cognome %><br>
  <b>Indirizzo:</b> <%= indirizzo %><br><br>
  <%
  }
} // end try
 
  catch (SQLException e) {
        %><b>Eccezione:</b> <%= e.toString() %><%
  } //end catch
 
  finally {
        if (connection != null) connection.close();
  } // end finally
%>

</body>

</html>


White R4bbit 08-18-2004 08:12 AM

Okay, I fixed it out!
It was a tomcat policy problem. I had to add these lines to /etc/tomcat4/policy.d/04webapps.policy
Code:

permission java.net.SocketPermission "127.0.0.1:3306", "connect,resolve";
  permission java.util.PropertyPermission "file.encoding", "read";

Hope it can help someone else :)

Haz 04-27-2005 08:31 AM

Thanks
 
Hi,

I had the same problem when I updated my version of Tomcat 4 in Debian. As the above quote mentioned I added the following lines to my /etc/tomcat4/policy.d/04webapps.policy file

Code:

permission java.net.SocketPermission "127.0.0.1:3306", "connect,resolve";
permission java.util.PropertyPermission "file.encoding", "read";

and it worked. I also commented out
Code:

#bind-address = 127.0.0.1
in my /etc/mysql/my.cnf

Thanks.


All times are GMT -5. The time now is 07:48 PM.