Java for Web Learning Notes (96): A tentative study of persistence (1) data storage

Source: Internet
Author: User
Tags couchbase mongodb object serialization amazon dynamodb value store
storage media for data flat-file Entity Storage

Different entity may be stored in different directories, each entity is a directory under a file, the name is the surrogate key (surrogate key), that is, index, convenient positioning. File format is diverse, may be direct Java object serialization, may be Xml,json.

Read and write entity, you need to open the file and close the file, so inefficient, and do not support the index outside the surrogate key. Typically used to store small amounts of basic data, such as application configurations. Structured file Storage

A structured file is a entity type that has multiple (all) Entity data of that type. In accordance with the agent key SK read a certain entity performance will have some problems, usually have a small index file, to help quickly in the large data file positioning.

This is similar to the ISAM (Indexed sequential Access method) in a relational database, as is the case with earlier databases such as Btrieve. The structure is private in a structured file and is parsed by program code, and the database is generic; More importantly, relational databases have associations between different entity, and structured files are not available. If we use ISAM in MySQL, the table does not support foreign keys. Relational Database Systems

Relational databases are the most commonly used databases, such as MySQL. Entity is stored as a record in a table, and the table is a entity type (of course, complex entity may require multiple tables), and tables are defined by a strict schema name, type, constraint, key. Use the ANSI standard structured Query Language (SQL) to manipulate table schemas and data. However, none of the relational databases are fully compliant with ANSI standards, mostly by their own extension, which makes migration difficult in different relational databases.

Using JDBC in Java (Java Database Connectivity), in order to avoid each access to the database, to reopen and shut down the connection of the database, the Third-party jar provides the management of the connection pool, the most common is the c3p0[1]. object-oriented Databases

Object-oriented database tries to solve the association problem of object-oriented and relational database. Both want to use SQL and want to get objects. But it's hard to get fish and paws. How to ensure the inheritance of an object, how to facilitate retrieval, and whether to use a private extension of SQL. These are all problems, and therefore, although there has been an object database, it has not been widely used. schema-less Database Systems

modeless database, which is not a strict schema. NoSQL is the description of such a database, developed rapidly in recent years. It solves the problem of flexible field and object integration in relational database. document-oriented databases have many types of NoSQL databases, the most common being document-oriented databases (document-oriented database), and data storage formats typically Xml,json (or Bson). A file corresponds to a record in the relational database, and a table in the corresponding relational database collection. Compared with the relational data, it is characterized by a much higher insert efficiency, but the query is not so fast, but the amount of data stored is much larger. Popular include Mongodb,apache Couch db and Couchbase Server. The Key-value store stores key-value pairs in some way, like Java map<string, String>, or map<string, list<string>>. Well-known key-value storage databases are: Apache Cassandra, Freebase, Amazon DynamoDB, Memcache, Redis, Apache River, Couchbase, and MongoDB. Some file databases also use key-value stores. Graphics databases focus on object relationships. Objects have attributes, as well as relationships with other objects, and relationships have attributes. The storage and presentation of data are graphs, and the association between entities is natural. The most popular graphics database is neo4j.

NoSQL database does not have similar ANSI SQL standard, no NoSQL database has its own clients, on the one hand, universality is OK, but most clients can read and write directly from the database, but do not need to map the Pojo class to the table as the relational database, map the attribute to the column. the traditional way of not mapping

The MySQL test database has a very simple students table with a lot more columns in the actual table.

CREATE TABLE ' Students ' (
  ' id ' int (one) not null auto_increment,
  ' name ' varchar ' (+) not NULL,
  ' score ' int (one) N OT NULL,
  PRIMARY KEY (' id ')
) Engine=innodb DEFAULT Charset=utf8
We used the C3P0 as a database connection management pool, previously written C3p0mysqlmanager [2], you can use, focus on reading and writing code:
"1" read example, student Pojo only three properties, relatively simple, if there are 10, dozens of, the code is verbose. Public Student getstudent (long id) throws sqlexception{try (Connection Connection = c3p0mysqlmanager.getconnection (DATA
			_source_name); PreparedStatement PS = connection.preparestatement ("select * from test.students WHERE ' id ' =?");)

		{Ps.setlong (1, id);
			Try (ResultSet rs = Ps.executequery ()) {if (!rs.next ()) return null;
			Student Student = new Student ();
			Student.setid (ID);
			Student.setname (rs.getstring ("name"));
			Student.setscore (Rs.getint ("score"));
		return student; An example of entity is added to the "2", in particular, this demonstrates how to get the automatically incremented serial number public void Addstudent (String name, int score) throws exception{Studen
	T student = new student ();
	Student.setname (name);
		
	Student.setscore (score);
			Try (Connection Connection = c3p0mysqlmanager.getconnection (data_source_name);
			                                                   PreparedStatement PS = connection.preparestatement ("INSERT into test.students (' name ', ' score ') VALUES (?,?)", StAtement.
		Return_generated_keys);) {ps.setnstring (1, Student.getname ());
						
		Ps.setint (2, Student.getscore ());		
			
		if (Ps.executeupdate ()!= 1) throw new Exception ("Failed to insert record.");
			Try (ResultSet r = Ps.getgeneratedkeys ()) {if (!r.next ()) throw new Exception ("Failed to retrieve ID.");  Student.setid (R.getlong (1)); can also be R.getlong ("Generated_key"), because R is only 1 columns, with columnindex more convenient}}}
Observe the MySQL interaction (set usessl=false in the URL) by grasping the packet, whether we set it to Statement.return_generated_keys or Statement.no_generated_keys in PS, Message messages are the same, with the last INSERT ID, only if the code is allowed to get through Ps.getgeneratedkeys ().



RELATED links: My professional Java for WEB applications related articles

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.