Hallo,
ich bin ziemlicher newbie was das alles hier angeht. und habe hier im forum auf der suche nach den im betreff genannten stichworten noch kein thema gefunden.
mit diesem einfachen java-programm, das ich aus einem tutorial habe, kann ich offensichtlich eine db-verbindung herstellen:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class DbVerbindung {
public static void main(String[] argv) {
System.out.println("Checking if Driver is registered with DriverManager.");
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe) {
System.out.println("Couldn't find the driver!");
System.out.println("Let's print a stack trace, and exit.");
cnfe.printStackTrace();
System.exit(1);
}
System.out.println("Registered the driver ok, so let's make a connection.");
Connection c = null;
try {
// The second and third arguments are the username and password,
// respectively. They should be whatever is necessary to connect
// to the database.
c = DriverManager.getConnection("jdbc :p ostgresql://localhost/testen", "postgres", "postgres");
} catch (SQLException se) {
System.out.println("Couldn't connect: print out a stack trace and exit.");
se.printStackTrace();
System.exit(1);
}
if (c != null)
System.out.println("Hooray! We connected to the database!");
else
System.out.println("We should never get here.");
}
}
result auf der console:
Checking if Driver is registered with DriverManager.
Registered the driver ok, so let’s make a connection.
Hooray! We connected to the database!
ich möchte das ganze aber für eine dynamische website machen und habe hierfür eine erstmal einfache html-datei erzeugt:
[html]
The servlet exampleA simple web application
[/html]und das java-programm entsprechend als servlet angepasst:import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class DbConnection extends HttpServlet {
//public static void main(String[] argv) {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Checking if Driver is registered with DriverManager.");
try {
Class.forName("org.postgresql.Driver");
out.println("found driver");
} catch (ClassNotFoundException cnfe) {
out.println("Couldn't find the driver!");
out.println("Let's print a stack trace, and exit.");
cnfe.printStackTrace();
System.exit(1);
}
out.println("Registered the driver ok, so let's make a connection.");
Connection c = null;
try {
// The second and third arguments are the username and password,
// respectively. They should be whatever is necessary to connect
// to the database.
c = DriverManager.getConnection("jdbc :p ostgresql://localhost/testen", "postgres", "postgres");
} catch (SQLException se) {
out.println("Couldn't connect: print out a stack trace and exit.");
se.printStackTrace();
System.exit(1);
}
if (c != null)
out.println("Hooray! We connected to the database!");
else
out.println("We should never get here.");
}
}
bis zum try läuft es und dann erhalte ich die meldung im browser “dieses programm kann die webseite nicht anzeigen”. in der console steht dann:
java.lang.ClassNotFoundException: org.postgresql.Driver
ich, als anfängerin, würde jetzt tippen, dass er den driver nicht findet. verstehe aber nicht warum, weil doch in der java-applikation funktioniert es. kann mir jemand helfen, wo mein fehler im programm bzw. in meiner denkweise ist?
vielen dank!
gruß katharina