CSCI 6962: Server-side Design and Programming Server-side Databases and Drivers

  • View

  • Download

Embed Size (px)

Text of CSCI 6962: Server-side Design and Programming Server-side Databases and Drivers

CSCI 6962: Server-side Design and Programming

CSCI 6962: Server-side Design and ProgrammingServer-side Databases and Drivers 11OutlineDatabase servers and driversDatabases and the MVC paradigmSQL overviewThe Derby database serverCreating databasesPopulating a databaseMySQL2Databases and E-CommerceLong term information stored in databases

Queries used to produce lists of products

Updates used to store ordersNew record created for order in Order table Customer information added to other tablesQuantities updated in inventory tables3Database ServersAccess to database controlled by database serverConstantly running (like web container)Server runs database queries and updates for databases it controlsServer handles security of databaseMost are password-controlled

MySQL (free)OracleMS ServerNot Access!Database serverWeb serverdatabaseDatabaseAccess Object4Programs create statement objects inside serverServer executes them on the database

Server stores results if query performedProgram may then access those resultsDatabase ServersDatabase serverdatabaseStatement objectselect * from booksResultSet objectproductCode title priceproductCode title priceproductCode title priceWeb serverDatabaseAccess Object5Database DriversDatabase server does not understand Java/C# commandsOnly understands its own DBMS protocolsEach server has its own DBMSNeed a database driver to perform translationObtain from database server providerInstall in Java librariesweb containerJSF pageManaged bean/ support classdatabase driverDBMSdatabase serverJDBC6Databases and the MVC ParadigmUsually do not directly access database from JSF/ASP pagesRequires both web programming and SQL knowledgeUsually done in model classes (such as managed bean)Bean stores to database when commanded toBean performs database query when need to retrieve dataOften have built-in tools for database managementSqlDataSource object in ASPweb containerdatabase serverJSF pages/Managed beanSupport classes/ toolsASP pages/Code behind7SQL and DatabasesCommands to database server use SQL (structured query language)

Common examples:Query for all records matching some condition:select field from table where condition

Example: select * from books where price < 10

Delete all records matching some condition:delete from table where condition

Example: delete from books where ISBN = 00048SQL and DatabasesSet new field value for all records matching some condition:update table set field = value WHERE condition

Example:update books set price = 9.95 where productCode = 0004

Insert new record with given field values:insert into table (field, field, field) values (value, value, value)

Example:insert into books (productCode, title, price) values (0004, Green Eggs and Ham, 9.95)9DerbyOpen source database server from ApacheNot as powerful as MySql, Oracle, etc.Low memory overheadFewer access privileges required

Included in later versions of NetBeansCreate database, create connection to itCreate tables and execute SQL commandsManipulate database from Java program running in NetBeans10Creating a Derby DatabaseGo to Services tabExpand Databases nodeRight-click Java DB nodeChoose Create Database

Creating a Derby DatabaseGive database a nameSet User name to appStrange schema errors otherwiseSet a password (I usually also just use app)

Creating a Derby DatabaseMust connect to the database before usingRight-click on link corresponding to databaseChoose Connect

Adding a TableExpand link to databaseExpand App nodeRight-click Tables nodeSelect Create Table Will get Create Table dialog

Adding a TableAdd column for each field of tableFirst field should be key fieldCheck Primary keyCheck UniqueSet types and other properties as needed# of characters in VARCHAREtc.

Adding a TableFinal table structure now shown in services window

Manipulating the DatabaseCan execute queries directly (without using code)Right click tableSelect Execute CommandBrings up command window

Type in SQL statementRight-click windowSelect Run Statement

Viewing the DatabaseRight click tableSelect View Data

Executes SELECT * FROM tablename query

MySQLFree database serverGo to www.mysql.comFollow Products tabGo to MySQL Community ServerDownload and run

MySQL Database DriverFrom downloads page at mysql.comGo to connectors pageGo to Connector/J (for Java)Download and saveExtract the mysql-connector-java-5.version-bin.jar file

MySQL Database DriverIn Netbeans right-click Libraries folder of projectChoose Add JAR/FolderSelect mysql-connector-java-5.version-bin.jar file from downloads

The MySQL InterfaceCommand line interface to manipulate databases MySQL controlsAvailable from PROGRAMS menuWill need to give password

Can create new databasescreate database databasename;

Creating Database TablesMust define field names and field typesSome types: varChar(n) : String of up to n characters int(n) : integer up to n digits double(n, d) : decimal number up to n digits and d after the decimalMust declare a key field with primary key create table tablename (keyfieldname type primary key,fieldname type, fieldname type,);