0

I am using eclipse luna and created a project on jsp but unable to call a class from "conect " package defined in src folder of my project

ConnectionProviderForSql.java

package connect;

    import java.sql.*;  

    public class ConnectionProviderForSql {  
    public static Connection con=null;  

    public static Connection getCon(){  
        try{  
            String DRIVER="com.mysql.jdbc.Driver";  
            String CONNECTION_URL="jdbc:mysql://localhost:3306/abc";  
            String USERNAME="username";  
            String PASSWORD="password";  

            Class.forName(DRIVER);  
            con=DriverManager.getConnection(CONNECTION_URL,USERNAME,PASSWORD);
            if(con!=null){
            System.out.println("connected");}
            else{
                System.out.println("not connected");
            }
            }catch(Exception e){e.printStackTrace();}  

        return con;  
    }  

    }  

my jsp file

reseller.jsp

<%@page import="connect.ConnectionProviderForSql"%>

<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
    Connection con1=null;
    Statement stmt =null;
    ResultSet rs = null;
    PreparedStatement ps=null;
    String result="bla";

    con1 = ConnectionProviderForSql.getCon();




        %>  

my browser output

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: [14] in the generated java file: [/root/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/work/Catalina/localhost/Panel/org/apache/jsp/reseller_jsp.java]
Only a type can be imported. connect.ConnectionProviderForSql resolves to a package

An error occurred at line: 17 in the jsp file: /reseller.jsp
ConnectionProviderForSql cannot be resolved
14:     PreparedStatement ps=null;
15:     String result="bla";
16:     
17:     con1 = ConnectionProviderForSql.getCon();
18:     

I had tried related question but it dint solve my problem Why it cannot resolve the class i have imported the package Please help trying this for hours. Thanks

deogratias
  • 472
  • 2
  • 8
  • 23

2 Answers2

1

The class file corresponding to ConnectionProviderForSql should be present in the WEB-INF/classes folder. Also, it should maintain the package folder structure. If you are using eclipse to compile and run a 'Java Web Project', ideally all these should happen automatically.

Girish
  • 93
  • 8
0

I would strongly not recommend to use scriplets...
Did you place ojdbc.jar into your lib folder and did you add it to your class path?

Why dont you use servlets?
Why dont you use JSTL?

Here take a look on that:
How to avoid Java code in JSP files?

Community
  • 1
  • 1