[Java] Java learning notes Summary (3)

Source: Internet
Author: User
Tags set set
Document directory
  • 2013-07-29
2013-07-291. What is socket? To develop a socket program based on the C/S structure, follow these steps.

A: socket is used to communicate with each other between two TCP/IP-based applications. It first appeared in UNIX systems and is the main information transmission method in UNIX systems. In Windows, socket is called Winsock.
Two basic concepts: customer and service provider. To use socket communication between two applications, you must first establish a socket connection between the two applications (either on the same machine or on different machines, the party initiating the call connection request is the customer, and the party receiving the call connection request becomes the service provider. The customer and the service provider are opposite. The same application can be the customer or service provider.
Before calling a connection request, the customer must know where the service provider is. Therefore, you need to know the IP address or machine name of the server on which the service provider is located. If the customer and the service provider have an agreement beforehand, this agreement is port (port number ). That is to say, the customer can call the service provider through the IP address of the server where the service provider is located, or the unique method for determining the machine name and port number. Before the customer calls, the service provider must be in the listening status to check whether a connection is required by the customer. Once a connection request is received, the service provider can establish or reject the connection as needed. There are two Connection Methods: synchronous and noblocking ).

To develop a socket program based on the C/S structure:

1) server (instance for creating serversocket) and client (for creating socket );

2) Open the input/output stream connected to the socket;

3) perform read/write operations on the socket according to certain protocols;

4) Disable socket.

2. How to traverse arraylist, vector, and hashset under the collection class. How to traverse hashmap. Write the core code. A: traverse the arraylist:

// Output all the elements of the collection in the public static void shuchu (Collection collection) {iterator = collection. iterator (); While (iterator. hasnext () {system. out. println (iterator. next ());}}

Traverse arraylist:

Vector v = new Vector();for (int index = 0; index < v.size(); index++) {String str = (String) v.elementAt(index);}

Traverse hashset:

Set set = new HashSet();for (int i = 0; i < 100; i++) {set.add("123");}for (Iterator it = set.iterator(); it.hasNext();) {System.out.println(it.next());}

Traverse hashmap:

Map<Integer, String> map = new HashMap<Integer, String>();  for(int i=0;i<100;i++)  {   map.put(i, "123");  }for(Entry<Integer, String> entry:map.entrySet())  {   System.out.println(entry.getKey()+"="+entry.getValue());  }

Addition, deletion, modification, and query of 2013-07-30sql

# Add insert into t_userr (username, password, sex) values ('JJ ', '11', 'n'); # delete from t_userr where username = 'JJ '; # update t_userr set ruxueyear = '000000', note = "utilities" where username = 'JJ '; # Check select ID, username from t_userr where sex = 'male '; select username from t_userr; select realname from t_userr where sex = 'male' and school = 'China Institute of Metrology '; select * From t_userr; # select * From t_userr where sex = 'femal' for all information ';

Add multiple records to the database using Java

Package COM. APP; public class grilfriend {public static void main (string [] ARGs) {basedao bdao = new basedao (); string [] secondclassify = {"Face Care ", "Eye lip Care", "Essential Oil spa", "makeup", "fragrance", "makeup tools"}; string [] [] thirdclassify = new string [secondclassify. length] [2]; string [] [] [] project = new string [secondclassify. length] [thirdclassify. length] [10]; int Ss = 1; for (INT I = 0; I <secondclassify. length; I ++) {for (Int J = 0; j <Thirdclassify [I]. length; j ++) {thirdclassify [I] [J] = (j = 0 )? "Category": "efficacy"; for (INT J2 = 0; J2 <10; J2 ++) {project [I] [J] [J2] = secondclassify [I] + thirdclassify [I] [J] + J2; object [] S3 = {SS ++, "Cosmetic", secondclassify [I], thirdclassify [I] [J], project [I] [J] [J2]}; bdao. update ("insert into girlfriend (ID, firstclassify, secondclassify, thirdclassify, Project) values (?,?,?,?,?) ", S3 );}}}}}

Queries the minimum birthday by category

SELECT * FROM t_userr WHERE birthdate in(SELECT MAX(birthdate) FROM t_userr GROUP BY sex);

Differences between 2013-07-31in and exists in

In can be divided into three types:
1. For example, select * from T1 where F1 in (& apos; A & apos;, & apos; B & apos;) should be more efficient than the following two
Select * from T1 where F1 = & apos; A & apos; or F1 = & apos; B & apos;
Or select * from T1 where F1 = & apos; A & apos; Union all select * from T1 F1 = & apos; B & apos;
You may not be referring to this category. We will not discuss it here.
2. For example, select * from T1 where F1 in (select F1 from T2 where t2.fx = & apos; X & apos ;),
The condition in the WHERE clause of the subquery is not affected by the outer query. In general, the automatic optimization is converted into an exist statement, that is, the efficiency is the same as that of the exist statement.
3. For example, select * from T1 where F1 in (select F1 from T2 where t2.fx = t1.fx ),
The condition in the WHERE clause of the subquery is affected by the outer query. The efficiency of such queries depends on the index and data volume of the fields involved in the related condition. Generally, it is considered that the efficiency is not as high as exists.
Except for the first type of in statements, they can be converted to exists statements. The general programming habit is to use exists instead of in.
Two tables A and B,
(1) When only one table's data such as a is displayed and only one relational condition such as ID is displayed, using in is faster:
Select * from a where ID in (select ID from B)
(2) When only the data of a table such as a is displayed, and the link condition such as ID and col1 is different, it is inconvenient to use in. You can use exists:
Select * from
Where exists (select 1 from B where id = A. ID and col1 = A. col1)
(3) when only two tables are displayed, the use of in and exists is not suitable and the connection is required:
Select * from a left join B on ID = A. ID
Therefore, the method used depends on the requirements.
Exists

Exists is used to determine whether a query exists. If the query result in exists (query) exists, true is returned. Otherwise, false is returned. Not exists is the opposite.
When exists is used as the where condition, the primary query before where is queried first, and then the query result of the primary query is substituted into the query of exists one by one for judgment, if it is true, the results of the current primary query will be output; otherwise, no output will be made.
Differences between in and exists

In is a hash connection between the external table and the internal table, while exists is a loop on the External table. Each loop then queries the internal table.
The argument that exists is more efficient than in is always inaccurate.
If the two tables to be queried are of the same size, there is little difference between in and exists.
If one of the two tables is small and the other is a large table, exists is used for the large subquery table, and in is used for the small subquery table.
Not exists: the usage of exists is different from that of in. Generally, it needs to be associated with sub-tables, and indexes must be used for association to speed up the process.

Exists is equivalent to existence quantizer: indicates that the Set exists, that is, the set is not empty and only applies to one set.
For example, exist P indicates true when P is not null; not exist P indicates true when P is null.
In indicates the relationship between a scalar and a dollar.
For example, s in P indicates that a value in S and P is true when it is equal; s not in P indicates that every value in S and P is not equal to true:

Not in and not exists
If the query statement uses not in, all the internal and external tables are scanned, and no index is used;
However, subqueries of not extsts can still use table indexes.
Therefore, whether the table is large, not exists is faster than not in.

2013-08-01mysql performance optimization 2013-08-02 achieve self-growth with Java code and add multiple records to the database at one time

Package COM. dao; import Java. util. *; public class test_oracle {static int id = new oracle_dao (). maxid ("select max (ID) from oracle_test") + 1; // static int id = 1;/** generates a random string, applicable to JDK 1.7 * // public static string random (INT length) {// stringbuilder builder = new stringbuilder (length); // For (INT I = 0; I <length; I ++) {// builder. append (char) (threadlocalrandom. current (). nextint (33,128); //} // return B Uilder. tostring (); //}/** generate a random string */public static string randomstring (INT length) {string STR = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 "; random random = new random (); stringbuffer Buf = new stringbuffer (); For (INT I = 0; I <length; I ++) {int num = random. nextint (62); Buf. append (Str. charat (Num);} return Buf. tostring ();}/** returns the subscript of the ID */public static int idnumber () {return ID + +;} Public static void main (string [] ARGs) {oracle_dao bdao = new oracle_dao (); string SQL = "insert into oracle_test (ID, username, password) select ?,?,? From dual "; string sqlstring =" Union select ?,?,? From dual "; arraylist <Object> alist = new arraylist <Object> (); alist. add (idnumber (); alist. add (randomstring (10); alist. add (randomstring (6); For (INT I = 0; I <19; I ++) {SQL = SQL + sqlstring; alist. add (idnumber (); alist. add (randomstring (10); alist. add (randomstring (6);} object [] objects = new object [alist. size ()]; for (INT I = 0; I <alist. size (); I ++) {objects [I] = alist. get (I);} bdao. update (SQL, objects); system. out. println (bdao. maxid ("select max (ID) from oracle_test "));}}

2013-08-031. Which of the following methods does Java connect to Oracle? Tell the difference and write the respective URLs. Answer: Method 1: connect to the database through the JDBC driver of the database.

Classs. forname ("oracle. JDBC. Driver. oracledriver ");

Connection conn = drivermanager. getconnection ("JDBC: oracle: thin: @ 192.168.1.33: 1521"

: Huihoo "," Scott "," Tiger ");

Method 2: connect to the database through the JDBC-ODBC Bridge

Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");

Connection conn = drivermanager. getconnection ("JDBC: ODBC: 192.168.1.33", "Scott", "Tiger ");

2. Grant the select and insert permissions for the table EMP to the Tom user and write the corresponding SQL statement.

A: grant select, update, insert, dellete on Scott. EMP to Tom;

3. State the code for the SQL injection vulnerability and provide a solution.

Http://blog.csdn.net/jueblog/article/details/97507372013-08-04

1. Find the first five youngest students born in Zhejiang.

A: oracle code:

Select * from (select * From t_user where home like '% Zhejiang %' order by birthdate DESC) Where rownum <6;

2. SQL of the Oracle paging program.

A:

(1) sample code using the pseudo-column rownum:

SELECT * FROM (SELECT ROWNUM rn,id,realname FROM (SELECT id,realname FROM T_USER)WHERE ROWNUM<=20) t2 WHERE T2.rn >=10;

(2) Use the analysis function row_number () over (order by field ):

After sorting by ID, 10 records are extracted from the first 10th records.

SELECT * FROM(SELECT id,realname,row_number()over(ORDER BY id asc) rn FROM T_USER)WHERE rn BETWEEN 10 AND 20;

(3) minus statement:

After sorting by ID, 10 records are extracted from the first 10th records.

(SELECT * FROM (SELECT * FROM T_USER ORDER BY id asc) WHERE ROWNUM<20)  MINUS( SELECT * FROM (SELECT * FROM T_USER ORDER BY id asc) WHERE ROWNUM<10);

3. Differences between stored procedures and functions, and the call methods in the console and Java language.

A: What are the differences between stored procedures and functions:

Stored Procedure

Function

Used to complete specific operations or tasks (such as insertion and deletion) in the database)

Used for specific data (such as selection)

Procedure for program header Declaration

Program header declaration function

Return type does not need to be described during program header Declaration

The return type must be described when the function header is declared, and at least one valid return statement must be included in the PL/SQL block.

You can use in/out/In out parameters.

You can use in/out/In out parameters.

It can be executed as an independent PL/SQL statement.

It cannot be executed independently and must be called as part of the expression

Zero or multiple values can be returned through out/In out.

Return a value using the return statement. The value must be consistent with the declared part. It can also be a variable that is taken out by an out parameter.

Stored procedures cannot be called in SQL statements (DDL or select ).

Functions can be called in SQL statements (DDL or select ).

Call a stored procedure in the console: Execute the stored procedure with the exec process name.

Call a function in the console: directly write an SQL statement to call this function.

Calling the stored procedure template in Java:

Callablestatement cs = conn. preparecall ("{call process (?, ?, ?)} ");

CS. setxxx (1, value 1 );

CS. setxxx (2, value 2 );

CS. registeroutparameter (3, types. Numeric );

Cs.exe cute ();

System. Out. println (CS. getint (3 ));

Call a function in Java: it is called using an SQL statement.

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.