本意是将网页上表格数据插入到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文件:
但还是没好,运行错误为