Java Code Examples for com.mysql.jdbc.Statement#executeUpdate()

The following examples show how to use com.mysql.jdbc.Statement#executeUpdate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UserDao.java    From StudentManagement with MIT License 6 votes vote down vote up
public User register(String username,String password,String level) {
	Connection conn = DBUtils.getConnection();
	User user = null;
	
	try {
		//判断数据库中是否存在该用户
		if(!userIsExist(username)){//不存在该用户,可以注册
			user = new User();//实例化一个user对象
			//给用户对象赋值
			user.setUsername(username);
			user.setPassword(password);
			user.setLevel(level);
			//将用户对象写入数据库
			Statement stmt = (Statement) conn.createStatement();
			stmt.executeUpdate("insert into user values('"+username+"','"+password+"','"+level+"');");
			stmt.close();//释放资源
		}
	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		DBUtils.closeConnection(conn);
	}
	return user;
}
 
Example 2
Source File: ConnMysql.java    From Leo with Apache License 2.0 6 votes vote down vote up
/**
 * 说明:用于执行插入、更新、删除的sql语句,当受影响的行数为0和执行失败时返回false
 * 
 * @param sql
 * @return boolean 成功返回true,失败返回false
 */
public boolean excSql(String sql) {
	if (con == null)
		getConnection(); // 连接到数据库

	try {
		Statement st = (Statement) con.createStatement(); // 创建用于执行静态sql语句的Statement对象
		int counts = st.executeUpdate(sql); // 执行操作的sql语句
		if (0 == counts) {
			log.info("执行成功,,共0条数据受到影响,没有完成操作!:SQL语句-->【" + sql + "】");
			return false;
		}
		log.info("执行成功,共" + counts + "条数据受到影响:" + "SQL语句-->【" + sql + "】");
		return true;
	} catch (SQLException e) {
		log.error("执行失败:SQL语句-->【" + sql + "】");
		log.error(e.getMessage());
		return false;
	}

}