50 likes | 151 Views
Classic Persistence. eric.gerlofsma@hu.nl www.ericgerlofsma.nl. CIJFERLIJST. TBL_Studenten. Naam. Cijfer. Example. Next example will show you, the classic database access, The database looks like this:. Updating a database(0). private static final String DSN_name = "CIJFERLIJST";
E N D
Classic Persistence eric.gerlofsma@hu.nl www.ericgerlofsma.nl
CIJFERLIJST TBL_Studenten Naam Cijfer Example • Next example will show you, • the classic database access, • The database looks like this: www.ericgerlofsma.nl
Updating a database(0) private static final String DSN_name = "CIJFERLIJST"; protected static final String usr = "efg"; protected static final String pwd = "geheim"; protected static final String url = "jdbc:odbc:" + DSN_name; protected static Object lock = new Object(); static { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (ClassNotFoundException cnfe) { System.err.println(cnfe); } } You need a driver to a database ! www.ericgerlofsma.nl
Updating a database(1) public void update(String name, String newName, String newCijfer) throws SQLException { String u = "UPDATE" + tablename + "SET " + tcol[0] + " = '" + newName + "'"; u += (", " + tcol[1] + " = '" + newCijfer + "'"); u += " WHERE " + tcol[0] + " = '" + name + "'"; executeUpdate(u); } You need a server call ! www.ericgerlofsma.nl
Updating a database(2) protected void executeUpdate(String s) throws SQLException { synchronized (lock) { Connection con = null; Statement stmt = null; try { con = DriverManager.getConnection(url, usr, pwd); stmt = con.createStatement(); stmt.executeUpdate(s); } finally { if (stmt != null) stmt.close(); if (con != null) con.close(); } } } And you need a database connection ! www.ericgerlofsma.nl