1. 程式人生 > >mysql數據庫__Jdbc直接操作__Statement__delete

mysql數據庫__Jdbc直接操作__Statement__delete

數據 trace user statement sys lock nec sta exce

一、代碼如下

private void deleteMysql() {
		// TODO Auto-generated method stub
		java.sql.Statement sm= null;
		java.sql.Connection cn = null;
		ResultSet rs = null;

		try {
			//加載驅動程序
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			//得到連接
			cn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/learn?user=root&password=&useUnicode=true&characterEncoding=UTF8");
			sm= cn.createStatement();

			int i= sm.executeUpdate("delete from user where username= ‘332‘");
			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 {
				if(sm != null) {sm.close();}
				if(cn != null) {cn.close();}} catch (SQLException e) {e.printStackTrace();}
		}
	}

mysql數據庫__Jdbc直接操作__Statement__delete