Salve ho creato una pagina jsp ma ho problemi di connessione:
ecco la classe che uso :
e la pagina jsp:codice:import java.io.FileNotFoundException; import java.io.IOException; import java.sql.*; import java.util.logging.Level; import java.util.logging.Logger; public class Db{ private Connection connect; public Connection connettialdatabase(String server,String nomedb,String username,String password){ try { try { Class.forName("com.mysql.jdbc.Driver") ; // Imposta la connessione al server e al database } catch (ClassNotFoundException ex) { Logger.getLogger(Db.class.getName()).log(Level.SEVERE, null, ex); } // prova="jdbc:mysql://"+server+"/"+nomedb+",\""+username+"\",\""+password+"\""; connect = DriverManager.getConnection("jdbc:mysql://"+server+"/"+nomedb,username,password); } catch (SQLException ex) { Logger.getLogger(Db.class.getName()).log(Level.SEVERE, null, ex); } return connect; } }
La classe e la pagina sarebbero più lunghe ma tutto è commentetao per testarle , inoltre ho fatto una classe test per provare la classe Db e questa mi rende la connessione perfettamente:codice:<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.sql.*" %> - <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <jsp:useBean id="db" scope="page" class="classi.Db" ></jsp:useBean> <% Connection connect= db.connettialdatabase("localhost","java_db","root","root"); out.println(connect); %> Torna a index </body> </html>
La pagina jsp mi rende invece null e un bel java.lang.ClassNotFoundException: com.mysql.jdbc.Drivercodice:import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; public class Test { /** * @param args * @throws IOException * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException, IOException { // TODO Auto-generated method stub Db d =new Db(); Connection connect =d.connettialdatabase("localhost", "java_db", "root", "root"); System.out.println(connect); } }
Quale potrebbe essere il problema?

Rispondi quotando


