1 / 5

Classic Persistence

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";

desma
Download Presentation

Classic Persistence

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classic Persistence eric.gerlofsma@hu.nl www.ericgerlofsma.nl

  2. CIJFERLIJST TBL_Studenten Naam Cijfer Example • Next example will show you, • the classic database access, • The database looks like this: www.ericgerlofsma.nl

  3. 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

  4. 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

  5. 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

More Related