Database queries

Suppose that you have to create an SQL query that includes the column names, table names, and literal values. The following code shows how you would typically write it, using traditional strings:

String query = "SELECT talk_title, speaker_name " + 
               "FROM   talks, speakers " + 
               "WHERE  talks.speaker_id = speakers.speaker_id " + 
               "AND    talks.duration > 50 "; 

You can also write the code as follows, using quotes around the column or table names, depending on the target database management system:

String query = "SELECT 'talk_title', 'speaker_name' " +
               "FROM   'talks', 'speakers' " + 
               "WHERE  'talks.speaker_id' = 'speakers.speaker_id' " + 
               "AND    'talks.duration' > 50 "; 

The raw string literal values are much more readable, as shown in the following code:

String query =  
```SELECT 'talk_title', 'speaker_name'  
   FROM   'talks', 'speakers'  
   WHERE  'talks.speaker_id' = 'speakers.speaker_id' 
   AND    'talks.duration' > 50  
```;

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.198.81