图书类
public class book {
private double price;
private String name;
private int count;
private String author;
private int id;
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
jsp前端输入存入数据库数据
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="Book.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="../addbook" method="post" οnsubmit="return check(this)">
图书名称:
<input type="text" name="name" />
价格:
<input type="text" name="price" />
数量:
<input type="text" name="count" />
作者:
<input type="text" name="author">
<input type="submit" value="添加">
</form>
</body>
</html>
向数据库增加图书信息
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="addbook",urlPatterns="/addbook") //xml映射 代替xml文件编写 urlPatterns为映射地址
public class addbook extends HttpServlet{
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
//获取jsp前端传输的数据
book bk=new book();
request.setCharacterEncoding("UTF-8"); //设置request的编码格式,防止中文传输乱码
bk.setName(request.getParameter("name"));
bk.setPrice(Double.valueOf(request.getParameter("price")));
bk.setCount(Integer.valueOf(request.getParameter("count")));
bk.setAuthor(request.getParameter("author"));
try{
//连接数据库
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/javas?useUnicode=true&characterEncoding=UTF-8";
//javas为数据库名
//?useUnicode=true&characterEncoding=UTF-8,设置编译为UTF-8,防止中文乱码
String username="root";
String password="";
Connection conn=DriverManager.getConnection(url,username,password);
String sql="insert into books(bookname,price,number,athour)values(?,?,?,?)";//?为占位符
PreparedStatement ps=conn.prepareStatement(sql);
//获取PreparedStatement,PreparedStatement继承于Statement接口,封装了jdbc执行sql语句的方法,可以使用?占位符来代替参数
ps.setString(1, bk.getName());
ps.setDouble(2, bk.getPrice());
ps.setInt(3,bk.getCount());
ps.setString(4,bk.getAuthor());
int row=ps.executeUpdate();//执行更新操作,返回所影响的行数
if(row>0){
System.out.println("成功添加了:"+row+"条数据!");
}
ps.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}