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

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com