201621123063 Summary of the 14th week of Java programming

Source: Internet
Author: User

1. Study summary 1.1 This week summarize your database-related content in the way you like (mind mapping or other).

2. Using database technology your system 2.1 briefly describes how to use the database to transform your system. What table do you want to build? The design of your watch.

The book borrowing system uses the database to store the library information, and establishes the Book information table .

idAs the primary key

To read a book table from the database:
Database diagram:

Successfully read and put into jtable display:

What are the key classes used in JDBC in the 2.2 system?

Connection: Get a connection to the database
DriverManger: Driver Manager, registration driver
PreparedStatement:Statement subclasses, pre-compiling SQL statements, making the database execute
ResultSet: Get query result set

2.3 Key code for database related modules. Critical lines need to be annotated.

3. Code volume Statistics 3.1 Statistics the amount of code completed this week

The weekly code statistics need to be fused into a single table.

Week Time Number of rows number of new rows Number of files number of new files
1 226 226 45 45
2 377 377 7 7
3 712 281 42 9
4 166 166 2 2
5 253 253 4 4
6 484 484 9 9
7 269 269 1 1
8 301 32 5 4
9 447 447 7 7
10 401 26 9 0
11 1262 1262 18 18
12 965 965 12 12
13 887 887 9 9
14 675 675 10 10
Option: 4. Database Learning Guide 1. MySQL Database basic operations

Create a database and insert your name and school number as a record. (, need to have their own school number, name)

Execute common SQL statements on a database that you build

2. Use the statement operation database with the JDBC Connection database and Statement2.1. Complete the experimental Task book-topic 2. One of the public static void Displayall () and public static int insert (Student stu) is the key code that appears with its own school number.

2.2 If you want to finish querying student data based on the specified name, complete the function public Student findstubyname (String name), where is the SQL statement written?
="select * from students WHERE name = "+name;
2.3 What do you think is the routine of using JDBC to manipulate databases?

① Load Database Driver
② establishing a database connection
③ Creating a Database Action object
④ SQL statements that define operations
⑤ Performing Database operations
⑥ getting and manipulating result sets
⑦close () Close Object
Reference: Experimental Task book-topic 2

3. PreparedStatement and parameterized Query 3.1 use PreparedStatement, write public Student findstubyname (String name) to find by name. (Paste the key code and run, need to appear in the operation of the number, comparison of 2.2, indicating the difference between the two implementations)
 Public StaticStudentFindstubyname(String name){String URL= "Jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8&usessl=false";String UserName= "Root";String Password= "123456";Connection Conn= NULL;PreparedStatement PreparedStatement= NULL;ResultSet ResultSet= NULL;Student Student= NULL;String SQL= "Select*from students where name =?";                Try {Conn= DriverManager.getconnection(URL,UserName,Password;PreparedStatement= Conn.preparestatement(SQL);            PreparedStatement.setString(1,Name;ResultSet= PreparedStatement.ExecuteQuery();             while(ResultSet.Next()){Student= New Student(ResultSet.getInt("id"),ResultSet.getString("Stuno"),ResultSet.getString("Name"),ResultSet.getInt("Age"),ResultSet.getDate("Birthdate"));            }        } Catch(SQLException e){            e.Printstacktrace();        } finally {             if(ResultSet!= NULL){                    Try {                        ResultSet.Close();                        ResultSet.Close();                    } Catch(SQLException e){                        e.Printstacktrace();                    }                }                if(PreparedStatement!= NULL){                    Try {                        PreparedStatement.Close();PreparedStatement= NULL;                    } Catch(SQLException e){                        e.Printstacktrace();                    }                }                if(Conn!= NULL)Try {                        Conn.Close();Conn= NULL;                    } Catch(SQLException e){                        e.Printstacktrace();                    }         }            returnStudent;    }

Run:

Different: PreparedStatement Executes Statement more efficiently, Statement each time the SQL statement is executed, the related database executes the sql statement compilation and Preparedstatement is precompiled.

3.2 If you want to implement Fuzzy Lookup, how to modify? such as writing public List
 Public StaticList<Student> Findstusbyname(String name){String URL= "Jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8&usessl=false";String UserName= "Root";String Password= "123456";Connection Conn= NULL;PreparedStatement PreparedStatement= NULL;ResultSet ResultSet= NULL;String SQL= "Select*from students where name like?";List<Student>Students= NewArrayList<>();        Try {Conn= DriverManager.getconnection(URL,UserName,Password;PreparedStatement= Conn.preparestatement(SQL);            PreparedStatement.setString(1, "%"+Name+"%");ResultSet= PreparedStatement.ExecuteQuery();             while(ResultSet.Next()){Student Student= New Student(ResultSet.getInt("id"),ResultSet.getString("Stuno"),ResultSet.getString("Name"),ResultSet.getInt("Age"),ResultSet.getDate("Birthdate"));                Students.Add(student);            }        } Catch(SQLException e){            e.Printstacktrace();        } finally {             if(ResultSet!= NULL){                    Try {                        ResultSet.Close();                        ResultSet.Close();                    } Catch(SQLException e){                        e.Printstacktrace();                    }                }                if(PreparedStatement!= NULL){                    Try {                        PreparedStatement.Close();PreparedStatement= NULL;                    } Catch(SQLException e){                        e.Printstacktrace();                    }                }                if(Conn!= NULL)Try {                        Conn.Close();Conn= NULL;                    } Catch(SQLException e){                        e.Printstacktrace();                    }         }        returnStudents;    }

Test data:

Run:

6. Transaction 6.1 Using code and running results proves that you did implement the transaction function and what do you need to be aware of when you encode the transaction? Note: To compare the changes made to the database in both the successful and failed run scenarios. (Paste a code that you think is more valuable, the number of the study)


Test data:

Before the change:

After the change:

Run failed: the data in the database has not changed

Note: database operations need to be step-by-steps, not multiple operations at the same time

6.2 When do you think you need to use transaction processing?

In order to ensure the consistency and correctness of data, it is necessary to use transaction processing for continuous operation of database data. When an operation fails on the data in the database, the transaction can roll back all the data to the data before the operation.

Optional: 5. Extracurricular reading 5.2 What does Dao,service,controller,util,model mean in the code structure, and why is it divided

Dao: Data access Object, which is responsible for database access
Service : Services, completion of some specified work
Controller : controllers, all instructions are issued by the controller
Util: tools that are commonly used to handle data that is not related to business logic
Model : models that describe the definition of a type of data, usually a collection of individual data

Division Benefits:
① partition code structure can make the Code good portability
② can make it easier for multiple people to collaborate on parallel development
③ can be developed with a clearer concept of the structure of the program

5.3 MYSQ Database management tools Navicat Basic usage methods


You can perform some general operations on the data directly in a visual chart, where 查询 you can use code statements to make a database available for pruning and transaction control

201621123063 Summary of the 14th week of Java programming

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.