摘要:本文主要向大家介绍了JAVA语言-JDBC基本增删改查操作讲解之增,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。
本文主要向大家介绍了JAVA语言-JDBC基本增删改查操作讲解之增,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。
图书类
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" "//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" onsubmit="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/javasuseUnicode=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();
}
}
}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号