The use of MySQL in statements in Java

Source: Internet
Author: User

When you do the permissions, you find the permissions for the roles in the Auth permissions through the Roleid in role roles.

SQL statements:

SELECT AuthName from Auth where authId in (1,2,3,4,5)

In the back of the thing is the role of the various permissions of the ID, you can find the 1,2,3,4,5 corresponding permissions name.

So the combination of Auth table, directly used, the SQL statement is:

Select AuthName from Auth where authId in (SELECT authids from role where roleid=1)

The 1,2,3,4,5 is the content that is found in the back. In practice, this can be an error, and the expected results cannot be detected. In the back of the content should be ("Field 1", "Field 2", ...), the above kind of check out, it should be this format in ("1,2,3,4,5"), the 1,2,3,4,5 five as a string.

Then in the Java code, you can avoid this problem, mainly because of the Java string concatenation, you can easily assemble in the following parentheses in the data format.

The following example code is to achieve the desired result.

 Public Static voidMain (string[] args)throwsException {//TODO auto-generated Method StubDbutil dbutil=NewDbutil (); Connection Connection=Dbutil.getcon (); String hql= "Select Authids from role where roleid=1"; PreparedStatement PreparedStatement2=connection.preparestatement (HQL); ResultSet ResultSet2=Preparedstatement2.executequery (); String Authid=NULL;  while(Resultset2.next ()) {Authid=resultset2.getstring ("Authids"); System.out.println ("Authids:" +resultset2.getstring ("Authids")); } String SQL= "Select AuthName from Auth where authId in (" +authid+ ")";//splicing SQL statements PreparedStatement PreparedStatement=connection.preparestatement (SQL); ResultSet ResultSet=Preparedstatement.executequery ();  while(Resultset.next ()) {System.out.println ("AuthName:" +resultset.getstring ("AuthName")); }    }

Related Article

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.