mercury841
11-11-2005, 19:51
Salve ho dei problemi a connettermi al Database Mysql,questa è la mia servlet:
import java.io.*;
import java.sql.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class es_database extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
// Carica (e quindi registra) il driver MySql
Class.forName("com.mysql.jdbc.Driver");
// Ottieni una connessione alla base di dati
con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/database","","");
// Crea un oggetto Statement
stmt = con.createStatement();
// Esegui una query SQL, ottieni un ResultSet
rs = stmt.executeQuery("SELECT * FROM tabella");
// Mostra l'insieme dei risultati come una lista
out.println("<HTML><HEAD><TITLE>Tabella Giuseppe</TITLE></HEAD>");
out.println("<BODY>");
out.println("<UL>");
while(rs.next()) {
out.println("<LI>" + rs.getString("name") + " " + rs.getString("phone"));
}
out.println("</UL>");
out.println("</BODY></HTML>");
}
catch (ClassNotFoundException e) {
// Handle an error loading the driver
out.println("Impossibile caricare il driver della base di dati: "
+ e.getMessage());
}
catch (SQLException e) {
// Handle an error getting the connection
}
finally {
// Chiudere sempre la connessione alla base di dati
try {
if (con != null)
con.close();
}
catch (SQLException ignored) { }
}
}
}
In poche parole non mi carica i driver della base dati.
Vorrei sapere perchè?
Spero di non suscitare in voi sentimenti di linciaggio...
Grazie ciao
import java.io.*;
import java.sql.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class es_database extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
// Carica (e quindi registra) il driver MySql
Class.forName("com.mysql.jdbc.Driver");
// Ottieni una connessione alla base di dati
con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/database","","");
// Crea un oggetto Statement
stmt = con.createStatement();
// Esegui una query SQL, ottieni un ResultSet
rs = stmt.executeQuery("SELECT * FROM tabella");
// Mostra l'insieme dei risultati come una lista
out.println("<HTML><HEAD><TITLE>Tabella Giuseppe</TITLE></HEAD>");
out.println("<BODY>");
out.println("<UL>");
while(rs.next()) {
out.println("<LI>" + rs.getString("name") + " " + rs.getString("phone"));
}
out.println("</UL>");
out.println("</BODY></HTML>");
}
catch (ClassNotFoundException e) {
// Handle an error loading the driver
out.println("Impossibile caricare il driver della base di dati: "
+ e.getMessage());
}
catch (SQLException e) {
// Handle an error getting the connection
}
finally {
// Chiudere sempre la connessione alla base di dati
try {
if (con != null)
con.close();
}
catch (SQLException ignored) { }
}
}
}
In poche parole non mi carica i driver della base dati.
Vorrei sapere perchè?
Spero di non suscitare in voi sentimenti di linciaggio...
Grazie ciao