简体   繁体   中英

Connecting to database using servlet

I am trying to make a simple application using servlets where I retrieve data from a user like name, surname, age and phone number which I then store in a database after insertion but I am having difficulties in doing it because when it comes to compiling and deploying using ant I get no errors but after clicking the submit button from my form, I get an error. I don't what might be the cause of the problem.I have both my servlet and database jar files in my lib folder before compiling and deployment but after deploye only one jar file is properly deploy.Any shed of light is really appreciated. Thank you in advance. I am thinking maybe my build.xml should be modified so both my jars files can be properly deployed. I get the following error.

I have included my build.xml file.


System for Mentoring 201103

<property name="name" value="servlet"/>
<property name="project.code" value="servlet"/>
<property name="project.name" value="servlet"/>
<property name="version" value="1.00.00"/>

<!--*** Source properties ***-->
<property name="src" location="src"/>
<property name="src-main" location="${src}/main"/>  
<property name="src-main.java" location="${src-main}/java"/>
<property name="src-main.resources" location="${src-main}/resources"/>
<property name="src-main.webapp" location="${src-main}/webapp"/>
<property name="src-test" location="${src}/test"/>      
<property name="src-test.java" location="${src-test}/java"/>
<property name="src-test.resources" location="${src-test}/resources"/>

<property name="lib" location="lib"/>

<!--*** Target properties ***-->
<property name="target" location="target"/>
<property name="build" location="${target}/build"/>
<property name="build.classes" location="${build}/classes"/>
<property name="deploy" location="${target}/deploy"/>
<property name="ide" location="${target}/ide"/>

<!--*** Tomcat Properties ***-->
<property name="tomcat-webapps" location="C:\apache-tomcat-7.0.37-windows-x64\apache-tomcat-7.0.37\webapps"/>

<!--*** Deploy properties ***-->
<property name="tomcat.deploy.dir" location="${tomcat-webapps}"/>

    <!--*** Classpath settings ***-->
<path id="classpath">
    <fileset dir="${lib}">
        <include name="**/*.jar"/>

    </fileset>
    <pathelement path="${build}/classes"/>
</path>

<!--***************************************************************************************************************
  * Target: Help (Default)
  ***************************************************************************************************************-->
<target name="help" description="Gives help on all tasks in this build file">
    <echo>Run "ant -projecthelp" or "ant -p" for a list of valid targets for this ant buildfile."</echo>
</target>

<!--***************************************************************************************************************
  * Target: createTempDirs
  ***************************************************************************************************************-->
<target name="init:create-temp-dirs" description="Creates all relevant temporary directoriees. Typically only used the first time these directories need to be created.">
    <mkdir dir="${target}"/>
    <mkdir dir="${build}"/>
    <mkdir dir="${build.classes}"/>
    <mkdir dir="${deploy}"/>
    <mkdir dir="${ide}"/>
    <mkdir dir="${ide}/classes"/>
</target>

<!--***************************************************************************************************************
  * Target: Clean
  ***************************************************************************************************************-->
<target name="clean" description="Deletes all relevant files and directories eg. build classes, build directories.">
    <!-- Kill the build directory -->
    <delete dir="${build}" />

    <!-- Delete backup files, if necessary -->

    <!-- Delete all temporary files, if necessary -->
</target>

<!--***************************************************************************************************************
  * Target: prepare
  ***************************************************************************************************************-->
<target name="prepare" depends="init:create-temp-dirs" description="Creates all relevant dirs, expand jars, etc.">
    <mkdir dir="${build}"/>
    <mkdir dir="${build.classes}"/>
</target>

<!--***************************************************************************************************************
  * Target: java
  ***************************************************************************************************************-->
<target name="_compile" depends="prepare" >
    <javac srcdir="${src-main.java}" destdir="${build.classes}">
        <classpath refid="classpath"/>
        <include name="**/*.java"/>
    </javac>
    <!-- copy all relevant confirguration files -->
    <copy todir="${build.classes}/resources">
        <fileset dir="${src-main}/resources"/>
    </copy>
    <!-- copy all file in root java directory. -->
    <copy todir="${build.classes}">
        <fileset dir="${src-main.java}" includes="*.*"/>
    </copy>
</target>

<target name="compile" depends="clean, _compile" 
          description="Does a clean Java compile ie. all previous built classes are first deleted and then a full recompile takes place." />
<target name="compile:fast" depends="_compile"
        description="Does a Java compile only on changed files. Previously compiled files are left untouched." />

<!--***************************************************************************************************************
  * Target: war related
  ***************************************************************************************************************-->
<property name="deploy.warfile.name" value="${deploy}/${project.name}.war"/>

<!-- Do a WAR build from the given build directory. No Hibernate build is done. -->
<target name="war:build" description="Creates the War file for this project.">
    <!--<delete file="${deploy.warfile.name}" />-->
    <war destfile="${deploy.warfile.name}" webxml="${src-main.webapp}/WEB-INF/web.xml">
        <classes dir="${build.classes}" />
        <webinf dir="${src-main.webapp}/WEB-INF" />
        <fileset dir="${src-main.webapp}" excludes="WEB-INF/**/*.*" />
        <lib dir="${lib}" includes="**/*.jar" excludes="servlet*.jar, jsp*.jar, javax*.jar" />
    </war>
</target>

<!-- Do a WAR Build and Deploy To Tomcat -->
<target name="war:deploy" depends="war:build" description="Builds the war file (war:build) and deploys it to Tomcat.">
    <copy file="${deploy.warfile.name}" todir="${tomcat.deploy.dir}" />
</target>

在此处输入图片说明

Please share your build.xml . I think your task which is creating the war file is not copying the jar files in war.

<target name="war" depends="build" description="War up the compiled classes into ${package.file.war}">
    <war destfile="${package.file.war}">
        <fileset dir="build" includes="**" excludes="**/*.java"/>       
    </war>
</target>

[EDIT]There is no issue in your build file. Make sure your source code contain this statement

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

You do not have mysql-connector jar file in your WEB-INF/lib folder

make sure you have the following things at place

1) in WEB-INF/lib folder copy paste all the jar dependencies you use

2) in WEB-INF/classes folder you should have your .class files

3) have your index.html or index.jsp file in Root folder of your web application

4) and have your web.xml in WEB-INF folder

you can find the required jar file in MySQL\\MySQL Tools for 5.0\\java\\lib folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM