-1

I have a problem with my progect. Files of progect:

House.class

 public class House implements Serializable {

    //properties -------------------------------------------------------------
    private String price;
    private String square;
    private String RoomNumbers;
    //------------------------------------------------------------------------

    //getters - settersm Object overriding.... -----------------------------

HouseDAO.class

public class HouseDAO {

    Connection connection;
    final String DB_CONNECTION = "jdbc:mysql://localhost:3306/mydb2";
    final String DB_USER = "root";
    final String DB_PASSWORD = "root";

    public HouseDAO(Connection connection) {
        this.connection = connection;
    }

    public List<House> getList() {
       List<House> houses = new ArrayList<>();
        try {

            connection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
            System.out.println("Connection available");
            PreparedStatement ps = connection.prepareStatement("SELECT Square, RoomNumbers, Price FROM houses  WHERE  District = 'Dnepr'");
            ResultSet rs = ps.executeQuery();
              while (rs.next()){
                 House house = new House();
                  house.setSquare(rs.getString("Square"));
                  house.setRoomNumbers(rs.getString("RoomNumbers"));
                  house.setPrice(rs.getString("Price"));
                    houses.add(house);
              }

        }catch (Exception ex){
            System.out.println("SQL exceprion");
            ex.printStackTrace();

        }
           return houses;
    }
}

and Servlet: HousesBaseServlet

@WebServlet("/post")
public class HosesBaseServlet extends HttpServlet {
    Connection conn;

    private HouseDAO houseDAO;
    @Override
    public void init(){
        houseDAO = new HouseDAO(conn);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
        //choice from html form
      //  String choice = request.getParameter("district");
        try {
            List<House> houses = houseDAO.getList();

            request.setAttribute("houses", houses);
            request.getRequestDispatcher("/houses.jsp").forward(request,response);


        }catch (Exception ex ) {
            System.out.println("Fail to connect with base");
             ex.printStackTrace();
        }
    }
}

I was read some solutiotuns, but it doesn't help. The problem in two exceptions:

SQL exceprion java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mydb2

I try to add:

Class.forName("com.mysql.jdbc.Driver");

to my code, and add mysql connector jar to my project, but it throws exception:

SQL exception java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

Second exception:

JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application Fail to connect with base

Here is my pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>


    </dependencies>

,

JSP taglib:

%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %

and project structure: [project structure][1]

Project structure - artifacts

In project structure -> libraries i have all jars.

user202822
  • 89
  • 1
  • 1
  • 8
  • 1
    your WEB-INF doesn't have any lib folder to place mysql jar – Ramanlfc Mar 05 '16 at 16:14
  • `@Override public void init(){ houseDAO = new HouseDAO(conn); }` is kind of strange what's the point of `conn` in this servlet. About your issue, I think Idea didn't "see" that this is a maven project, try reimporting it –  Mar 05 '16 at 16:15
  • doesn't help. Problem only with using tomcat, in standart project with MAIN class all works – user202822 Mar 05 '16 at 16:31
  • Check that the MySQL jar is present - that is the error. You could make it `runtime` as JDBC does not compile against a vendor's API/jar. For the current JDBC 4 one will no longer need `Class.forName` to load the driver oneself dynamically. Last remedy place the jar in WEB-INF/lib/. – Joop Eggen Mar 05 '16 at 18:55

1 Answers1

1

Since you are using IntelliJ I believe you might need to add the libraries to the artifact because from my experience Intellij adds the maven dependencies to the Classpath but not to the artifact.

Make sure you go to File -> Project Structure -> Artifacts and then add all the libraries from the available side to the artifact.

But you need to register the driver before getting the connection otherwise it doesn't work either way :

Class.forName("com.mysql.jdbc.Driver");     
connection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);

Hope this helps.

Tiago
  • 598
  • 9
  • 15
  • I has just started to learn JavaEE, before adding jars to "libraries" in project structure. What is Artifacts? There was error in Artifacts window, which called about missing jdbc driver. I click on button "Fix it", but exceptions didn't miss. After that i made double click on jars in Artifacts -> availible elements , and my second exception was miss. But not with driver: No suitable driver found for jdbc:mysql://localhost:3306/mydb2 – user202822 Mar 05 '16 at 21:12
  • I updated question, added picture with artifacts structure – user202822 Mar 05 '16 at 21:18
  • Inside the WEB-INF do you have the jdbc driver? And are you connecting as a datasource when tomcat starts? Try copying the jdbc driver jar to tomcat's lib directory, should be something like tomcat/lib/ if you are running it as a datasource, tomcat connects to the database and makes a pool even before the app is deployed. PS: sorry for not being exact, only have tomcat projects at work – Tiago Mar 05 '16 at 21:22
  • Wow, after i remove jars from artifacts and register driver Class.forName("com.mysql.jdbc.Driver") project start work. I don't now how) Thank you – user202822 Mar 05 '16 at 21:29
  • Well if it works it works, do you have it in tomcat lib folder? – Tiago Mar 05 '16 at 21:30
  • In tomcat/lib i have tomcat-jdbc.jar, but i wasn' add it. – user202822 Mar 05 '16 at 21:34
  • Ah that makes sense then, I use sql server, so I need a specific .jar that's why I usually need to past it there, you should take at look at connection pools and datasources, it's a better way to handle database connections like it's explained there http://stackoverflow.com/questions/13792469/basic-db-connection-pool-with-java-and-tomcat-7 And if you dont need it in the artifact you should put the maven dependency as provided provider – Tiago Mar 05 '16 at 21:36
  • thanks, awesome answer! – Martin Pfeffer Oct 03 '17 at 09:21