A database
JDBC with H2: the jar comes from Maven Central. Queries, parameters, transactions and metadata.
What you will learn#
jMavento download a package,jSqlConnect,jSqlScript,jSqlQuery,jSqlValue,jSqlExec,- parameters (
?) instead of text joined into SQL, - a transaction that rolls back.
The program#
# Tutorial 12: a database - JDBC with H2 (the jar comes from Maven Central)
load "java.ring"
jMaven("com.h2database:h2:2.2.224") # downloaded the first time to ~/.ringjava/maven
# 1. connect: an in-memory database that lives as long as the program
oDb = jSqlConnect("jdbc:h2:mem:tutorial")
# 2. a script: several statements in one call
jSqlScript(oDb,"
create table product (id int primary key auto_increment, name varchar(40), price decimal(8,2), stock int);
insert into product (name, price, stock) values ('Pen', 1.50, 100);
insert into product (name, price, stock) values ('Notebook', 4.25, 40);
insert into product (name, price, stock) values ('Backpack', 39.90, 5)
")
# 3. a query: a list of rows, each row is a list of [name, value] pairs; aRow[:name] finds a column
aRows = jSqlQuery(oDb,"select * from product order by price",[])
for aRow in aRows
? "" + aRow[:id] + " " + aRow[:name] + " " + aRow[:price] + " " + aRow[:stock]
next
# 4. values go to the ? marks: never join them into the SQL text (that is how SQL injection happens)
? jSqlValue(oDb,"select name from product where price > ? and stock > ?",[3,10])
# 5. changing rows: jSqlExec gives the number of rows that changed
? jSqlExec(oDb,"update product set stock = stock - ? where name = ?",[3,"Pen"])
? jSqlExec(oDb,"delete from product where stock < ?",[10])
? jSqlValue(oDb,"select count(*) from product",[])
# 6. a transaction: all or nothing
oDb.setAutoCommit(false)
try
jSqlExec(oDb,"insert into product (name, price, stock) values (?, ?, ?)",["Eraser",0.5,50])
jSqlExec(oDb,"insert into product (name, price, stock) values (?, ?, ?)",[NULL,"not a number",1])
oDb.commit()
catch
oDb.rollback()
? "rolled back"
done
oDb.setAutoCommit(true)
? jSqlValue(oDb,"select count(*) from product",[])
# 7. the JDBC objects are Java objects: metadata of a table
oMeta = oDb.getMetaData()
oColumns = oMeta.getColumns(NULL,"PUBLIC","PRODUCT","%")
while oColumns.@next()
? oColumns.getString("COLUMN_NAME") + " " + oColumns.getString("TYPE_NAME")
end
oColumns.close()
oDb.close()1 Pen 1.50 100
2 Notebook 4.25 40
3 Backpack 39.90 5
Notebook
1
1
2
rolled back
2
ID INTEGER
NAME CHARACTER VARYING
PRICE DECIMAL
STOCK INTEGERHow it works#
jMaven("com.h2database:h2:2.2.224") downloads the jar and its dependencies the first time and puts them on the class path. A query gives a list of rows, each row a list of [name, value] pairs, so aRow[:name] finds a column. The values always go to the ? marks: joining them into the SQL text is how SQL injection happens. In a transaction a failing statement undoes all the changes.
Exercises#
- Add a table of customers and join it with the products.
- Print the names of the columns of a table from its metadata.
- Show that a rollback restores the old rows.