Implement SQL, how to choose in Java

In general, I want to know how SQL is implemented at the lower level, it looks like

the algorithm used is close to O (1) ..... in Java, you can only achieve this with

hashmaps, I wander how they did it

Example:

If I have a group of students and a group of classes to select any of the students who belong to those classes

or any class contains a group of students, I would create a relational database in which there are three tables:

student table, class table, relationship table

which should represent a good many-to-many relationship

however, if I do not want to use SQL, JDBC, create and create tables

how can i implement this in pure java

I would like something like

List<Student> getStudentsByClass(String className) 

or

  List<Class> getClassesByStudent(String StudentName) 

Ideally, I will have hashMap using a unique studentID as a key, and the actual studentObject as a value and another hasMap uing classID as key and classObject as the value

then the ArrayList relation contains all the relation objects, inside the relation object you have 2 files, classID and studentID

the problem is that I don't want to iterate over the ArrayList relation every time I do a search

I know that there is a method for comparing an object that I can override with one that only helps you sort obejcts; it does not help with the choice of not?

there is a link, I understand everything, but not a choice of bits, not one of them, please consult!

http://www.javaworld.com/javaworld/jw-11-2004/jw-1122-select.html?page=3

+4
source share
5 answers

You can iterate over your "table" and check each element according to the specified criteria. This will work with O (n) complexity. If you want your program to create indexes faster using Map (e.g. HashMap or TreeMap) (O (1)) or / and sort the data and use binary search (log (n)).

+2
source

Serializing objects may work for you here ...

 public static void testSerialization() { String filename = "person.txt"; StudentDetails s1 = new PersonDetails("hemanth", 10, "Male"); StudentDetails s2 = new PersonDetails("bob", 12, "Male"); StudentDetails s3 = new PersonDetails("Richa", 10, "Female"); List list = new ArrayList(); list.addAll(Arrays.asList(s1, s2, s3)); FileOutputStream fos = null; ObjectOutputStream out = null; try { fos = new FileOutputStream(filename); out = new ObjectOutputStream(fos); out.writeObject(list); out.close(); System.out.println("Object Persisted"); } catch (IOException ex) { ex.printStackTrace(); } } 

Of course, reading will be very similar. Unfortunately, getting "elective" requests on this is not trivial. I would suggest looking at the H2 database. It is trivially easy and works very well. Here is a small example that creates a database, a table, inserts material and reads everything again.

 public static void h2test() throws SQLException { JdbcDataSource ds = new JdbcDataSource(); ds.setURL("jdbc:h2:testdb/somedb"); Connection connection = ds.getConnection(); PreparedStatement statement = connection.prepareStatement("CREATE TABLE IF NOT EXISTS TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))"); System.out.println(statement.execute()); statement = connection.prepareStatement("INSERT INTO TEST VALUES(10, 'ten')"); System.out.println(statement.executeUpdate()); connection.commit(); statement = connection.prepareStatement("SELECT * FROM TEST"); ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { System.out.println(resultSet.getInt(resultSet.findColumn("ID")) + ", " + resultSet.getString(resultSet.findColumn("NAME"))); } } public static void main(String[] args) throws SQLException { System.out.println("Hello world"); h2test(); } 

I really recommend it.

0
source

You can simply create multiple indexed views to satisfy the queries you want to resolve.

 HashMap<String, List<Student>> studentsByClassId; HashMap<String, List<SchoolClass>> classesByStudentId; HashMap<String, Student> studentByStudentId; HashMap<String, SchoolClass> classByClassId; 

Keep them in a single class that acts as your level of data access and provides search methods as you suggest. CRUD actions go through this class.

If you later want to upgrade to a database implementation, the implementation of your data access class will change. But if you did a reasonable job, then the interface will not.

0
source

I would suggest Guava . I do not have complete information to give you a complete example, but in guava you can create Predicates that will act as filters.

I like this tutorial he did on guava, the link to the url here .

To give you an example of a possible use, you can specify a method to return the predicate for you, as

 public static Predicate<String> isStartsWith(final String input){ return new Predicate<String>() { @Override public boolean apply( String str ) { if(str.startsWith( input )) return true; return false; } }; } 

and then call the method as such

 filteredList.addAll( Collections2.filter( youList, isStartsWith("thing you want to filter") ) ); 
0
source

Do you find using Hibernate ? It allows you to use SQL-like queries (HQL) directly on Java objects. This was usually backed up by a "real" database, but you don't need it. In fact, in one of my previous professional experiences, we used it with a database in memory ( HSQLDB ) very successfully for this unique reason: the ability to perform complex queries (including aggregation, federation, etc.) on very large collections of Java objects.

0
source

Source: https://habr.com/ru/post/1382733/


All Articles