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);//跳转到查询所有得服务程序
    	}
    
    }
    
  • 相关阅读:
    【leetcode】974. Subarray Sums Divisible by K
    【leetcode】976. Largest Perimeter Triangle
    【leetcode】973. K Closest Points to Origin
    listen 70
    科学60秒 (一) :上
    listen 69
    listen 68 Theoretical Physicist Stephen Hawking Dies at 76
    中译英33
    listen 67
    中译英32
  • 原文地址:https://www.cnblogs.com/max-hou/p/10849017.html
Copyright © 2011-2022 走看看