Nutz after the insert operation, returns the inserted Id_dao

Source: Internet
Author: User

Nuts Insert a database in the following ways:


Dao.insert ();

Dao.excute (SQL);

and other ways. However, you cannot return the inserted ID.

The following methods can be implemented:

/**t_plan_keyword table id*/
private static Long Keywordid =-1;
/**
 * Insert database according to keyword and return ID
 * @param dao
 * @param keyword keyword
 * @return
/public static Long Insertkeyword (Dao dao,final String keyword) {
	dao.run (new Conncallback () {
		@Override public
		Void Invoke (Connection conn) throws Exception {
			String SqlString = "INSERT into T_plan_keyword (keyword) VALUES ('" +keywor D+ "')";
			Statement stmt = Conn.createstatement ();
			Stmt.executeupdate (SqlString, Statement.return_generated_keys);
			ResultSet rs  = Stmt.getgeneratedkeys ();
			if (Rs.next ()) Keywordid =  Rs.getlong (1)	;
			if (Rs!= null) rs.close ();
			if (stmt!= null) stmt.close ();
		}
	});
	return keywordid;
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.