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);//跳转到查询所有得服务程序
    	}
    
    }
    
  • 相关阅读:
    Oracle EBS OM 主要API示例
    WIP 投料报 Invalid Serial Number
    物料事务处理interface与temp解析
    INV_TXN_MANAGER_PUB.PROCESS_TRANSACTIONS
    FND Debug Log(FND_LOG_MESSAGES)
    Oracle Apps DBA 常用命令
    详解EBS接口开发之WIP模块接口
    使用Java管理千台规模Linux服务器_入门
    windows下spark开发环境配置
    零成本实现Android/iOS自动化测试:基于Appium和Test Perfect
  • 原文地址:https://www.cnblogs.com/max-hou/p/10849017.html
Copyright © 2011-2022 走看看