LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-17-2004, 03:35 PM   #1
White R4bbit
Member
 
Registered: Aug 2003
Location: Italy
Distribution: Debian sid 2.6.1
Posts: 54

Rep: Reputation: 15
[ 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
 
Old 08-17-2004, 07:34 PM   #2
Jose Muņiz
Member
 
Registered: Jul 2003
Location: Mexico City
Distribution: Slackware 9.1, SuSE 9.1
Posts: 248

Rep: Reputation: 32
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?
 
Old 08-18-2004, 04:02 AM   #3
White R4bbit
Member
 
Registered: Aug 2003
Location: Italy
Distribution: Debian sid 2.6.1
Posts: 54

Original Poster
Rep: Reputation: 15
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>
 
Old 08-18-2004, 08:12 AM   #4
White R4bbit
Member
 
Registered: Aug 2003
Location: Italy
Distribution: Debian sid 2.6.1
Posts: 54

Original Poster
Rep: Reputation: 15
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
 
Old 04-27-2005, 08:31 AM   #5
Haz
LQ Newbie
 
Registered: Apr 2005
Posts: 1

Rep: Reputation: 0
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
java + connection to mysql linux_ub Programming 5 03-04-2012 10:59 AM
Mysql and mysql java connector, connection refused arubin Slackware 3 03-29-2008 01:41 AM
path to jsp/java? chadi Linux - General 0 11-18-2004 09:17 PM
MySQL/Java connection problem hudy Programming 3 06-23-2004 08:40 PM
MySQL connection over TCP/IP fails Rukawa Linux - Software 4 11-29-2003 11:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration