Java 使用servlet连接到数据库
我尝试使用servlet制作一个简单的应用程序,从用户名、姓氏、年龄和电话号码中检索数据,然后在插入后将其存储在数据库中,但我很难做到这一点,因为在使用ant编译和部署时,我没有遇到任何错误,但在单击表单中的提交按钮之后,我犯了一个错误。我不知道问题的原因是什么。在编译和部署之前,我的lib文件夹中有servlet和数据库jar文件,但在部署之后,只有一个jar文件得到了正确部署。非常感谢您的帮助。先谢谢你。我想也许我的build.xml应该修改,这样我的两个jars文件都可以正确部署。我得到以下错误 我已经包含了build.xml文件Java 使用servlet连接到数据库,java,servlets,ant,Java,Servlets,Ant,我尝试使用servlet制作一个简单的应用程序,从用户名、姓氏、年龄和电话号码中检索数据,然后在插入后将其存储在数据库中,但我很难做到这一点,因为在使用ant编译和部署时,我没有遇到任何错误,但在单击表单中的提交按钮之后,我犯了一个错误。我不知道问题的原因是什么。在编译和部署之前,我的lib文件夹中有servlet和数据库jar文件,但在部署之后,只有一个jar文件得到了正确部署。非常感谢您的帮助。先谢谢你。我想也许我的build.xml应该修改,这样我的两个jars文件都可以正确部署。我得到以
辅导制度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>
运行“ant-projecthelp”或“ant-p”获取此ant构建文件的有效目标列表。”
您的
WEB-INF/lib
文件夹中没有mysql连接器jar文件
确保你有以下物品在适当的地方
1) 在WEB-INF/lib
文件夹中复制粘贴您使用的所有jar依赖项
2) 在WEB-INF/classes
文件夹中,应该有.class文件
3) 将index.html
或index.jsp文件放在web应用程序的根文件夹中
4) 并将web.xml放在web-INF
文件夹中
您可以在
MySQL\MySQL Tools for 5.0\java\lib
文件夹中找到所需的jar文件,请共享您的build.xml。我认为您创建war文件的任务不是在war中复制jar文件
<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>
@Bhavik,谢谢你的回复。我的lib文件夹中确实有servlet 2.5 jar和mysql connectort jar文件,问题是在我部署之后,war文件中只正确部署了一个库,而另一个库不存在。因此我不知道我的build.xml是否可能是原因,因为我在build.xml文件下有以下内容。@Sohailehmad谢谢你的回复。我已经放置了build.xml我想你缺少Class.forName(“com.mysql.jdbc.Driver”);我设法解决了这个问题,当我在构建文件中创建war文件时,我排除了某些jar文件名,因此我意识到我也排除了编译所需的一个库,因此它只部署了mysql连接器jar文件,而没有部署servlet库,因为它与我之前使用的名称相同包括,我把它从排斥中移除,我工作了。再次非常感谢Sohailehmad。
Class.forName("com.mysql.jdbc.Driver");