zoukankan      html  css  js  c++  java
  • JDBC ----- SQL 插入记录

    package demo;
    
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class AddCustomer
     */
    @WebServlet("/addCustomer.jsp")
    public class AddCustomer extends HttpServlet {
    	private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public AddCustomer() {
            super();
            // TODO Auto-generated constructor stub
        }
    
    	/**
    	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		//增加
    		//获取浏览器输入的参数
    		String CustomerID =request.getParameter("CustomerID");
    		String CustomerName = request.getParameter("CustomerName");
    		String ContactName= request.getParameter("ContactName");
    		String Address = request.getParameter("Address");
    		String City = request.getParameter("City");
    		String PostalCode = request.getParameter("PostalCode");
    		String Country=request.getParameter("Country");
    		
    		//设置数据库连接参数
    		String url="jdbc:mysql://localhost:3306/库名?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";
    		String user = "账号";
    		String password="密码";
    		
    		//加载数据库驱动
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		}
    		try(Connection connection =DriverManager.getConnection(url, user, password)){//连接数据库
    			//设置插入sql语句,Customers为表名
    			 String sql = "INSERT INTO Customers(CustomerName,ContactName,Address,City,PostalCode,Country) VALUES (?,?,?,?,?,?);";
    	            PreparedStatement statement = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);//预处理sql语句
    				
    					//以下语句对应VALUES语句里的值
    			        statement.setString(1, CustomerName);
    			        statement.setString(2, ContactName);
    			        statement.setString(3, Address);
    			        statement.setString(4, City);
    			        statement.setString(5, PostalCode);
    			        statement.setString(6, Country);
    			        
    			        int value = statement.executeUpdate();
    			        
    			        statement.close();// 关闭statement,释资源
    			        
    		}catch(SQLException e) {
    			e.printStackTrace();
    		}
    		request.getRequestDispatcher("queryalldata.jsp").forward(request, response);//跳转到查询所有得服务程序
    	}
    
    }
    
  • 相关阅读:
    代码块;继承;this与super关系;重载与重写对比;类的继承特点;final关键字 (Java Day08)
    变量访问;this关键字;静态;工具类;帮助文档;Math使用;Arrays使用(Java Day07)
    面向对象;类和对象;访问对象;创建对象在内存中的理解;匿名对象;封装和this (Java Day06)
    如何保证 RocketMQ 不丢失消息
    Java-String类型的参数传递问题
    图解前中后序遍历
    一文彻底理解ReentrantLock可重入锁的使用
    聊聊MySQL、HBase、ES的特点和区别
    MySQL vs Java 数据类型
    Multi-Tenancy多租户模式
  • 原文地址:https://www.cnblogs.com/max-hou/p/10849017.html
Copyright © 2011-2022 走看看