一、代码如下

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();
			}
		}
	}

 

相关文章:

  • 2021-07-05
  • 2021-12-04
  • 2021-10-03
  • 2022-01-30
  • 2021-12-15
  • 2021-09-29
  • 2021-12-08
猜你喜欢
  • 2022-12-23
  • 2021-10-02
  • 2022-12-23
  • 2021-12-10
  • 2021-07-11
  • 2021-06-18
相关资源
相似解决方案