Documente Academic
Documente Profesional
Documente Cultură
• Relational Databases
• JDBC
What is a database?
• An ANSI standard
• BUT: most vendors have their own extensions to
the standard.
• However, they all support at least the 4 most
important commands:
– SELECT
– UPDATE
– DELETE
– INSERT
Activity – Postgresql installation
Ex:
create database books;
drop database books;
SQL– sample commands
SET autocommit=0;
start transaction;
do stuff;…
commit;
OR
rollback;
j2db
try {
Class.forName("org.postgresql.Driver").newInstance();
} catch (InstantiationException|IllegalAccessException|ClassNotFoundException
e){
System.err.println("Can’t load driver. Verify CLASSPATH");
System.err.println(e.getMessage());
}
JDBC – Create a connection
“
JDBC – Query result
PreparedStatement ps = null;
try {
ps = conn.prepareStatement("insert into authors (name, email, about, birthdate) values (?, ?, ?, ?)");
ps.setString(1, "Sample name");
ps.setString(2, "sample.name@email.com");
ps.setString(3, "About sample author");
ps.setDate(4, java.sql.Date.valueOf("1982-10-23"));
ps.executeUpdate();
} catch (SQLException e) {
System.err.println("Cannot insert author: " + e.getMessage());
} finally {
if (ps != null) try { ps.close(); } catch (SQLException e) { }
}
PreparedStatement
• The cursor can be moved to various positions in the result set: first(),
last(), previous(), relative(int n), absolute(int n)
• Remember in MySQL:
set autocommit = 0; …stmt… commit OR rollback;
• http://www.sqlcourse.com/
• http://www.w3schools.com/sql/
• https://jdbc.postgresql.org/documentation/
documentation.html
• http://www.tutorialspoint.com/postgresql/
• http://www.oracle.com/technetwork/java/ja
vase/jdbc/index.html
• https://docs.oracle.com/javase/tutorial/jdbc
/basics/
• http://www.tutorialspoint.com/jdbc/