本意是将网页上表格数据插入到mysql的表中
数据获取和类的构造 L_datainsert.java
package a;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public abstract class L_datainsert implements javax.servlet.Servlet {
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html");
request.setCharacterEncoding("UTF-8");
String[] thi=request.getParameterValues("thi");
String[] the=request.getParameterValues("the");
String[] fl=request.getParameterValues("fl");
String[] chu=request.getParameterValues("chu");
String[] hh=request.getParameterValues("hh");
for(int n=0;n<10;n++){
Hs hs = new Hs();
hs.setThi(Double.parseDouble(thi[n]));
hs.setThe(Double.parseDouble(the[n]));
hs.setFl(Double.parseDouble(fl[n]));
hs.setChu(Double.parseDouble(chu[n]));
hs.setHh(Double.parseDouble(hh[n]));
Hsinsert db = new Hsinsert();
boolean canLogin = db.addHs(hs);}
}
}
class Hs
{
private double thi;
private double the;
private double fl;
private double chu;
private double hh;
public Hs(double i, double e, double l,double u, double h)
{
thi = i;
the = e;
fl = l;
chu = u;
hh = h;
}
public Hs()
{
}
public void setThi(double thi){
this.thi = thi;
}
public double getThi(){
return thi;
}
public void setThe(double the){
this.the = the;
}
public double getThe(){
return the;
}
public void setFl(double fl){
this.fl = fl;
}
public double getFl(){
return fl;
}
public void setChu(double chu){
this.chu = chu;
}
public double getChu(){
return chu;
}
public void setHh(double hh){
this.hh = hh;
}
public double getHh(){
return hh;
}
}
插入方法实现:Hsinsert.java
package a;
import java.sql.*;
public class Hsinsert {
boolean bInited = false;
//加载驱动
public void initJDBC() throws ClassNotFoundException {
//加载MYSQL JDBC驱动程序
Class.forName("com.mysql.jdbc.Driver");
bInited = true;
System.out.println("Success loading Mysql Driver!");
}
public Connection getConnection() throws ClassNotFoundException,
SQLException{
if(!bInited){
initJDBC();
}
//连接URL为 jdbc:mysql//服务器地址/数据库名
//后面的2个参数分别是登陆用户名和密码
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/login?characterEncoding=utf8&useSSL=true","root","");
return conn;
}
public boolean addHs(Hs hs){
boolean returnValue = false;
try{
Class.forName("com.mysql.jdbc.Driver");//加载数据库驱动,注册到驱动管理器
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/login?characterEncoding=utf8&useSSL=true","root","");
//创建connection连接,
String sql="insert into hs(thi,the,fl,chu,hh) values(?,?,?,?,?)";
//添加图书信息的sql语句
PreparedStatement ps=conn.prepareStatement(sql);
//获取PreparedStatement
ps.setDouble(2,hs.getThi());//对sql语句中的第1个参数赋值
ps.setDouble(3,hs.getThe());//对sql语句中的第2个参数赋值
ps.setDouble(4,hs.getFl());//对sql语句中的第3个参数赋值
ps.setDouble(5,hs.getChu());//对sql语句中的第4个参数赋值
ps.setDouble(6,hs.getHh());
int row=ps.executeUpdate();//执行更新操作,返回所影响的行数
if(row>0){
returnValue = true;
}
ps.close();
conn.close();
}catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}
return returnValue;
}
}
但是运行没办法实现 弹出
是因为我没有设置好xml文件吗 还是别的原因?我的xml文件内容如下:
工程内容截图为:
我重新配置了xml文件:
但还是没好,运行错误为
弹出的结果是:
HTTP Status 404 – Not Found
Type Status Report
Message /login/L_datainsert
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists
把你项目的jsp所在工程目录截图来看看
找不到网址 你的页面网址有问题 与后台代码暂时无关
tomcat启动成功但是访问方面都是404
http://blog.csdn.net/shasiqq/article/details/51302632
web.xml的里面servlet配置的是什么,根据这个来访问servlet
请求名找不到 对比一下你请求的浏览器地址和web.xml里面配置的servlet 是不是一样的 不然访问不到
把你的servlet贴出来看看就是你的loginServlet.java类
HTTP Status 404 – Not Found
Type Status Report
Message /login/L_datainsert
你web.xml配置的请求地址是/login当然会找不到啦
500说明你servlet里面写的代码有问题
/WEB-INF/classes目录下有没有生成对应的class文件了
没有在工程的WEB-INF目录中,没有生成classes,会报这样的错误。解决方法:project->properties->java build path->source->src,将Default output folder设置为[项目名]/WebContent/WEB-INF/classes,点击OK。
你试试行不行
大佬们,我重新写了个servlet然后配置XML,已经可以了,谢谢各位大佬的回答