PDA

View Full Version : [JAVA] problema connessione a MYSQL


bobby1112
28-05-2009, 22:19
salve ragazzi non riesco a connettermi con una servlet ad un DB MYSQL

ecco la classe:


import com.mysql.jdbc.Connection;
import com.sun.xml.rpc.tools.wscompile.Main;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author katone
*/

public class Connessione {
//Creazione singleton
private static Connessione connessione = null;

public synchronized static Connessione newConnessione() throws ServletException {
if (connessione == null) connessione = new Connessione();
return connessione;
}
public Connection c =null;


public Connessione() throws ServletException {
try {
Class.forName("com.mysql.jdbc.Driver");
c=(Connection)DriverManager.getConnection("jdbc:mysql://localhost/BD-name?user=user&password=xxx");
}catch(ClassNotFoundException e) {
e.printStackTrace();
throw new ServletException("Errore",e);
} catch(SQLException e) {
e.printStackTrace();
throw new ServletException("Errore di connessione al database",e);
}
}

//Chiusura della connesione col db
protected void finalize() throws Throwable {
super.finalize();
if (c!=null) c.close();
}



public ResultSet GenericQuery(String _sql){
Statement s = null;
ResultSet r;
try{
s = (Statement) c.createStatement();
r = s.executeQuery(_sql);
return r;
}catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public static void main(String [] args)
{
try {
Connessione c = new Connessione();
} catch (ServletException ex) {
Logger.getLogger(Connessione.class.getName()).log(Level.SEVERE, null, ex);
}
}

}
dove ce DB-name metto il nome del DB,user metto l' user eccccccc

mi va sempre nell' eccezione Errore di connessione al database:muro: