zoukankan      html  css  js  c++  java
  • mysql数据库__Jdbc直接操作__PreparedStatement__insert

    一、代码如下

    private void insert_Mysql() {
    		// TODO Auto-generated method stub
    		java.sql.PreparedStatement ps = null;
    		java.sql.Connection cn = null;
    		ResultSet rs = null;
    
    		try {
    			// Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    			Class.forName("com.mysql.jdbc.Driver").newInstance();
    			cn = DriverManager.getConnection(
    					"jdbc:mysql://localhost:3306/learn?user=root&password=&useUnicode=true&characterEncoding=UTF8");
    			ps = cn.prepareStatement("insert into user(username, password, phone, email) values(?, ?, ?, ?);");
    
    			ps.setString(1, "wjb");
    			ps.setString(2, "wjb");
    			ps.setString(3, "wjb");
    			ps.setString(4, "wjb");
    			
    			int i= ps.executeUpdate();
    			System.out.println(i);
    			if(i>=1) {
    				System.out.println("新增:成功");
    			}else {
    				System.out.println("新增:失败");				
    			}
    
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} finally {
    			try {
    				ps.close();
    				cn.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    private void insert_Mysql_Two() {
    		// TODO Auto-generated method stub
    		java.sql.PreparedStatement ps = null;
    		java.sql.Connection cn = null;
    		ResultSet rs = null;
    
    		try {
    			// Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    			Class.forName("com.mysql.jdbc.Driver").newInstance();
    			cn = DriverManager.getConnection(
    					"jdbc:mysql://localhost:3306/learn?user=root&password=&useUnicode=true&characterEncoding=UTF8");
    			ps = cn.prepareStatement("insert into user(username, password, phone, email) values('332', '332', '33', '322');");
    
    			
    			
    			int i= ps.executeUpdate();
    			System.out.println(i);
    			if(i>=1) {
    				System.out.println("新增:成功");
    			}else {
    				System.out.println("新增:失败");				
    			}
    
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} finally {
    			try {
    				ps.close();
    				cn.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
  • 相关阅读:
    delphi与JAVA的webservice架构
    process执行DOS命令
    DELPHI+JAVA 之间使用WebService
    一步一步用delphi实现webservice
    集合接口
    再次郑重声明
    京东的crm做的不错,赞一个
    vs 2010 ClickOnce 发布设置界面,系统必选包中缺少 fx2.0 的解决方法
    使用vs2010生成clickonce安装引导程序的方法
    用 Axialis IconWorkshop 将ico图标转换成gif图标
  • 原文地址:https://www.cnblogs.com/wujianbo123/p/7637037.html
Copyright © 2011-2022 走看看