使用jsp在class.forName()上的ClassNotFOundException

时间:2017-02-05 11:37:07

标签: java jsp servlets jdbc

请解决我的问题我正在使用sql server从db获取值但是我得到以下异常我是我的dbConnect类:

第一页是我的index.jsp而不是

stateDAO.java是一个servlet类:

response.setContentType("text/html")"
PrintWriter out = response.getWriter();
Connection conn;
Statement st=null;
        try {
            conn=dbConnection.connect();

            String state_name= request.getParameter("stateId");

            System.out.println("state name is "+state_name);

            ArrayList State_all=null;

            ArrayList State_id= new ArrayList();

            String query_state="select * from state_master";

            if(state_name != null && !state_name.equals("")) {

                query_state = "select * from master_state where state_name='" + state_name + "'" ;

                System.out.println("query " + query_state);
                    st = conn.createStatement();
                    ResultSet rs = st.executeQuery(query_state);

                    while (rs.next()) {

                        State_all=new ArrayList();

                        State_all.add(rs.getString(1));
                        State_all.add(rs.getString(2));
                        State_all.add(rs.getString(3));
                        State_all.add(rs.getString(4));
                        State_id.add(State_all);

                    }

                    request.setAttribute("State_name", State_id);

                    RequestDispatcher success= request.getRequestDispatcher("success.jsp");

                    success.forward(request, response);

                    conn.close();


                        }
            }


         catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

}

provider interface 

public interface provider {

    String username= "sa";
    String Password = "sql@123";
    String url = "jdbc:sqlserver://localhost\\sqlexpress";


}

dbConnection.java

public class dbConnection implements provider {

    static Connection conn=null
        public static Connection connect() throws ClassNotFoundException {

            try {

                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
              conn = DriverManager.getConnection(url, username, Password);
                System.out.println("Connected to the  server successfully.");
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }

            return conn;
        }

    }

如果我没有使用class.ForName()我收到以下错误“找不到合适的jdbc:sqlserver://localhost\sqlexpress"驱动程序

请帮助我,我提前两天在这个问题上挣扎

0 个答案:

没有答案