Connexion à une base de données PostgreSQl à partir d'une SERVLET java
cs_power97
Messages postés14Date d'inscriptionjeudi 10 mai 2012StatutMembreDernière intervention 1 juin 2012
-
31 mai 2012 à 12:34
Pschh
Messages postés6Date d'inscriptionvendredi 11 mars 2005StatutMembreDernière intervention 1 juin 2012
-
1 juin 2012 à 11:45
Bonjour à tous
Je veux créer une application java web avec le SGBD postgreSQL. Pour ce faire:
- J'ai installé postgreSQL8
- J'ai également inclu le pilote postgresql.jar dans le lib du projet.
Voici le code de ma servlet
Quand je lance l'exécution de ma servlet voici l'erreur qui s'affiche sur ma console:
1 mai 2012 11:22:55 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: "Servlet.service()" pour la servlet uploadfichier a généré une exception
java.lang.NullPointerException
at web.ServletUpload.doGet(ServletUpload.java:47)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
Comment je peux contourner ça?
Merci
public void init() throws ServletException{
//String pilote = getInitParameter("jdbc.Driver");
String pilote="org.postgresql.Driver";
//String BaseDonnees = getInitParameter("localisation");
System.out.println("0: Je me connecte a la BD");
try{
System.out.println("1: Je me connecte12 a la BD");
System.out.println("pilotes= "+pilote);
Class.forName(pilote);
//Class.forName("com.postgresql.Driver");
System.out.println("2: Je me connecte a la BD");
Connection connexion=(Connection) DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "postgre");
instruction = connexion.prepareStatement(requete);
System.out.println("3: Je me connecte a la BD");
}
catch(ClassNotFoundException e){
System.out.println("DESOLE: Connexion impossible");
// log("Driver BD Non trouve");
//throw new ServletException();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public void doGet(HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException{
try{//System.out.println("3: Je suis dans la methode GET");
instruction.setString(1,(request.getParameter("numero")));
instruction.setString(2,request.getParameter("nom"));
instruction.setString(3,request.getParameter("chemin"));
instruction.executeUpdate();
request.setAttribute("reponse", "L'enregistrement s'est faite avec succes");
request.setAttribute("passe","Je suis satisfait");
try {
getServletContext().getRequestDispatcher("reponse.jsp").forward(request, response);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch(SQLException e){ log("Personne non enregistre");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
doGet(request,response);
}
/*public void destroy(){
if(connexion !=null){
try {
connexion.close();
}
catch(SQLException e){
log("Erreur de fermeture de la BD");
}
}
}*/
}
cs_Julien39
Messages postés6414Date d'inscriptionmardi 8 mars 2005StatutModérateurDernière intervention29 juillet 2020369 1 juin 2012 à 10:39
Pour une base oracle, tu as ce genre d'import à réaliser :
import oracle.jdbc.pool.OracleDataSource;
Dans ton cas, c'est un peu différent mais je ne vois pas d'import avec odbc ou jdbc. Je pense que tu n'as pas ajouté ce package à ton classpath et comme tu catch les ClassNotFoundException, tu ne vois pas l'erreur comme tu le devrais.
Pschh
Messages postés6Date d'inscriptionvendredi 11 mars 2005StatutMembreDernière intervention 1 juin 2012 1 juin 2012 à 11:45
Salut,
pas besoin d'import...
Il apparait que tu ne passes pas par la methode init() avant de faire le doGet et que tu fais "instruction.setString" sur instruction initialisée à null.
Essayes en début de methode doGet, qq chose comme ça:
if (instruction == null) init();