Results 1 to 3 of 3
  1. #1
    initmax's Avatar
    initmax is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default J2EE = Servlet+Mysql connect

    Hello All!
    I need your help to solve my question.
    I developed a Servlet + jsp application.
    I tested it in local with Apache Tomcat/6.0.20 and it works correctly.
    I write and use these classes:


    /*
    * Class CreateBaseConnection provide connect to base MySQL
    *
    *
    package MySQL;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;


    /**
    *
    * @author initmax
    */
    public class MySQLConnect {

    private String url = null;
    private String name = null;
    private String password = null;



    public MySQLConnect()
    {
    url = "jdbc:mysql://localhost:3030/initmax"+"?autoReconnect=true&useUnicode=true&char acterEncoding=utf8"+
    "&SET NAMES utf8";
    name = "root";
    password = "pass";
    }



    public MySQLConnect(String a_url, String a_name, String a_password){
    url = a_url;
    name = a_name;
    password = a_password;
    }


    //************************************************** *********************
    private Connection CurrentConnect;

    //this line need out to txt file


    //************************************************** *********************
    //first need download driver, for work MySQL connect
    public void DownloadDriver()
    {
    try
    {
    Class.forName("com.mysql.jdbc.Driver");
    // System.out.println("Driver loading success!");
    }

    catch (ClassNotFoundException e)
    {
    e.printStackTrace();
    }
    }

    //************************************************** *********************
    //Connected to MySQL Base use download driver
    public void Connected()
    {
    try
    {
    CurrentConnect = DriverManager.getConnection(url, name, password);
    // System.out.println("Connected.");
    }

    catch (SQLException e)
    {
    e.printStackTrace();
    }
    }


    //************************************************** *******************
    //disconnected base after finish work, get object connect
    public int DisConnected()
    {
    try
    {
    CurrentConnect.close();
    System.out.println("Disconnected.");
    }

    catch (SQLException e)
    {
    e.printStackTrace();
    }

    return 0;
    }

    //************************************************** *******************
    //get current connection
    public Connection GetConnection()
    {
    return this.CurrentConnect;
    }

    }




    package MySQL;

    /*
    * Class for create page, based MySQL data
    */


    //import java.sql.Connection;
    //import java.io.*;

    /**
    *
    * @author initmax
    */
    public class GenPageMySQL {

    private int id=0;
    private String theme=null;
    private String page=null;
    private String column=null;


    public GenPageMySQL(){

    }


    public GenPageMySQL(int a_id, String a_theme, String a_page){
    id = a_id;
    theme = a_theme;
    page = a_page;
    }


    /**
    * @return the id
    */
    public int getId() {
    return id;
    }

    /**
    * @param id the id to set
    */
    public void setId(int id) {
    this.id = id;
    }

    /**
    * @return the theme
    */
    public String getTheme() {
    return theme;
    }

    /**
    * @param theme the theme to set
    */
    public void setTheme(String theme) {
    this.theme = theme;
    }

    /**
    * @return the page
    */
    public String getPage() {
    return page;
    }

    /**
    * @param page the page to set
    */
    public void setPage(String page) {
    this.page = page;
    }

    /**
    * @return the column
    */
    public String getColumn() {
    return column;
    }

    /**
    * @param column the column to set
    */
    public void setColumn(String column) {
    this.column = column;
    }

    }



    package MySQL;

    /*
    * Class for create MySQL query
    */

    import java.util.*;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    /**
    *
    * @author initmax
    */
    public class MySQLQuery {

    private Connection CurrentConnect;
    private Vector VectorPageObj = new Vector();


    public MySQLQuery() {}


    public MySQLQuery(Connection CurrentConnectBase)
    {
    CurrentConnect = CurrentConnectBase;
    }

    //************************************************** ***************

    public Vector SelectAllField(String NameTable)
    {
    try
    {

    Statement st = CurrentConnect.createStatement();
    String query = ("select * from "+NameTable);
    ResultSet resultQuery = null;
    resultQuery = st.executeQuery(query);


    if(resultQuery == null) { return VectorPageObj; }


    while (resultQuery.next())
    {
    GenPageMySQL PageObj = new GenPageMySQL();

    PageObj.setId(resultQuery.getInt("id"));
    PageObj.setTheme(resultQuery.getString("theme"));
    PageObj.setPage(resultQuery.getString("page"));

    VectorPageObj.add(PageObj);
    }


    }
    catch (SQLException e) {
    e.printStackTrace();
    }

    return VectorPageObj;
    }



    /**
    *@set the CurrentConnect
    */
    public void setConnection(Connection CurrentConnectBase) {
    CurrentConnect = CurrentConnectBase;
    }

    }


    And Servlet "start point"

    import java.util.*;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import MySQL.*;

    public class indexServlet extends HttpServlet {

    private String getpage;
    private MySQLConnect MySQLConnectObj = new MySQLConnect();
    private MySQLQuery MySQLQueryObj = new MySQLQuery();


    public void init(){
    MySQLConnectObj.DownloadDriver();
    MySQLConnectObj.Connected();


    MySQLQueryObj.setConnection(MySQLConnectObj.GetCon nection());
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, NullPointerException {
    init();


    PrintWriter out = response.getWriter();
    getpage = request.getParameter("page");


    out.print("MySQLConnectObj.GetConnection() = "+MySQLConnectObj.GetConnection());



    if(MySQLConnectObj.GetConnection() != null)
    {

    Vector VectorPage = new Vector();
    VectorPage = MySQLQueryObj.SelectAllField("up_menu");

    GenPageMySQL PageObj = new GenPageMySQL();
    for(int i=0; i<VectorPage.size(); ++i)
    {
    // PageObj = (PageObj)VectorUpMenuObj.elementAt(i);
    request.setAttribute("up_menu_theme",VectorPage.el ementAt(i));

    RequestDispatcher Dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/jsp/index.jsp");
    Dispatcher.forward(request, response);
    }

    }


    else if(MySQLConnectObj.GetConnection() == null){


    out.print("MySQLConnectObj.GetConnection() = "+MySQLConnectObj.GetConnection());

    init() ;
    }


    return;
    }

    }

    If i start this classes in Java Application, connect MySQL and query works.
    But if I start this project how J2ee in NetBeans, get error in log Tomcat


    28.10.2009 15:41:58 org.apache.catalina.core.AprLifecycleListener init
    INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.5.0_06\bin;.;C:\WINDOWS\system32;C :\WINDOWS;C:\Program Files\PC Connectivity Solution\;C:\Java\jre6\bin;;;C:\Program Files\Java\jdk1.5.0_06\bin
    28.10.2009 15:41:58 org.apache.coyote.http11.Http11Protocol init
    INFO: Initializing Coyote HTTP/1.1 on http-8080
    28.10.2009 15:41:58 org.apache.catalina.startup.Catalina load
    INFO: Initialization processed in 899 ms
    28.10.2009 15:41:58 org.apache.catalina.core.StandardService start
    INFO: Starting service Catalina
    28.10.2009 15:41:58 org.apache.catalina.core.StandardEngine start
    INFO: Starting Servlet Engine: Apache Tomcat/6.0.20
    28.10.2009 15:41:59 org.apache.catalina.core.StandardContext resourcesStart
    SEVERE: Error starting static Resources
    java.lang.IllegalArgumentException: Document base K:\NetBeans_project\FirstServlet\build\web does not exist or is not a readable directory
    at org.apache.naming.resources.FileDirContext.setDocB ase(FileDirContext.java:142)
    at org.apache.catalina.core.StandardContext.resources Start(StandardContext.java:4048)
    at org.apache.catalina.core.StandardContext.start(Sta ndardContext.java:4217)
    at org.apache.catalina.core.ContainerBase.addChildInt ernal(ContainerBase.java:791)
    at org.apache.catalina.core.ContainerBase.addChild(Co ntainerBase.java:771)
    at org.apache.catalina.core.StandardHost.addChild(Sta ndardHost.java:526)
    at org.apache.catalina.startup.HostConfig.deployDescr iptor(HostConfig.java:630)
    at org.apache.catalina.startup.HostConfig.deployDescr iptors(HostConfig.java:556)
    at org.apache.catalina.startup.HostConfig.deployApps( HostConfig.java:491)
    at org.apache.catalina.startup.HostConfig.start(HostC onfig.java:1206)
    at org.apache.catalina.startup.HostConfig.lifecycleEv ent(HostConfig.java:314)
    at org.apache.catalina.util.LifecycleSupport.fireLife cycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1053)
    at org.apache.catalina.core.StandardHost.start(Standa rdHost.java:722)
    at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(Stan dardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(Sta ndardService.java:516)
    at org.apache.catalina.core.StandardServer.start(Stan dardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalin a.java:583)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at org.apache.catalina.startup.Bootstrap.start(Bootst rap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstr ap.java:413)
    28.10.2009 15:41:59 org.apache.catalina.core.StandardContext start
    SEVERE: Error in resourceStart()
    28.10.2009 15:41:59 org.apache.catalina.core.StandardContext start
    SEVERE: Error getConfigured
    28.10.2009 15:41:59 org.apache.catalina.core.StandardContext start
    SEVERE: Context [/FirstServlet] startup failed due to previous errors
    28.10.2009 15:41:59 org.apache.catalina.core.StandardContext stop
    INFO: Container org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/FirstServlet] has not been started
    28.10.2009 15:42:01 org.apache.coyote.http11.Http11Protocol start
    INFO: Starting Coyote HTTP/1.1 on http-8080
    28.10.2009 15:42:01 org.apache.jk.common.ChannelSocket init
    INFO: JK: ajp13 listening on /0.0.0.0:8009
    28.10.2009 15:42:01 org.apache.jk.server.JkMain start
    INFO: Jk running ID=0 time=0/156 config=null
    28.10.2009 15:42:01 org.apache.catalina.startup.Catalina start
    INFO: Server startup in 2502 ms
    28.10.2009 15:42:02 org.apache.catalina.startup.HostConfig checkResources
    INFO: Undeploying context [/initmax]
    28.10.2009 15:42:02 org.apache.catalina.core.StandardContext start
    INFO: Container org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/initmax] has already been started

    what could be wrong?

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    Document base K:\NetBeans_project\FirstServlet\build\web does not exist or is not a readable directory
    It's likely this folder does not exist anymore ?
    The netbeans IDE has a settings menu for how to boot up the servlet container, an embedded tomcat server within netbeans. try to set the folder to be something else. if not, its possible the way it tries to run the application outside of a real servlet container deployment is misbehaving for some reason.

  3. #3
    initmax's Avatar
    initmax is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default J2EE = Servlet+Mysql connect

    Thanks you, trouble determination, need install jdk1.6.

Similar Threads

  1. J2EE = Servlet+Mysql connect
    By initmax in forum Java Servlet
    Replies: 2
    Last Post: 10-30-2009, 08:57 AM
  2. connect java with mysql in rhel5
    By niraj.vara in forum New To Java
    Replies: 0
    Last Post: 07-17-2009, 10:23 AM
  3. Replies: 15
    Last Post: 04-15-2009, 02:51 AM
  4. Connect applet with mysql
    By baltimore in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 07:39 AM
  5. Connect applet with mysql
    By Felissa in forum Java Applets
    Replies: 3
    Last Post: 07-05-2007, 07:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •