|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
html data extraction and insert to db
I have a jtidy program that produces xhtml or just well formed html. It streams the output as a file. I would like to use regex to find patterns in the html and extract data between these files: Something like
<tag> data </tag> once I have the data, which almost all of it is text. I would like to use may a preparedStatement to insert the data into an oracle table. I need example on how to extract the data and insert using preparedStatements or just statements. Thanks |
|
#2
|
||||
|
||||
|
I'll give an example about updating. For inserting, you just have to change the SQLstatement. This code is part of a DAO (Data Access Object) which is called by the servlet. The servlet receives the request (through submitting of the form in the JSP), decides where the application should go, and then gives the request to the DAO.
public Land updateLand(HttpServletRequest _request ) throws SQLException { if (_request == null) returnnull ; else { HttpSession sessie = _request.getSession(false); if (sessie == null) returnnull; else { Land land = (Land) sessie.getAttribute("land"); if (verschilLandEnFormgegevens(_request) ) { Connection con = null; PreparedStatement ps = null; try { StringBuffer strUpdate = new StringBuffer(); strUpdate.append("update land "); strUpdate.append(" set naam=?, landcode=?, opdelingGewenst=?, "); strUpdate.append(" titelOpdeling=? "); strUpdate.append(" where ID = ? "); con = getConnection(); ps = con.prepareStatement(strUpdate.toString()); String formNaam = _request.getParameter("naam"); String formLandcode = _request.getParameter("landcode"); String formOpdeling = _request.getParameter("opdeling"); String formNaamOpdeling = _request.getParameter("naamOpdeling"); boolean opdeling = true; if (formOpdeling == null) opdeling = false; ps.setString(1, formNaam); ps.setString(2, formLandcode); ps.setBoolean(3, opdeling); ps.setString(4, formNaamOpdeling); ps.setInt(5, land.getID()); ps.executeUpdate(); return zoekLandOpID(land.getID()); } catch (SQLException SQLEx) { throw SQLEx; } finally { ps.close(); con.close(); } } elsereturn land; /* object "land" hasn't changed */ } /* session != null */ } } // end updateLand |
|
#3
|
||||
|
||||
|
ABout the getConnection() in the previous example : I use a GeneralDao where I define just once how to connect to the database. All the other DAO's are subclasses of this general DAO. And so, it's sufficient to write con = getConnection();
protected Connection getConnection() throws java.sql.SQLException { Connection dbconn = null; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); dbconn = DriverManager.getConnection("jdbc:mysql://localhost/brukkerlin"); } catch (InstantiationException e) { System.out.println("GeneralDao - Fout bij getConnection - instantiation " ); e.printStackTrace(); } catch (IllegalAccessException e) { System.out.println("GeneralDao - Fout bij getConnection - illegal acces " ); e.printStackTrace(); } catch (ClassNotFoundException e) { System.out.println("GeneralDao - Fout bij getConnection - class not found " ); e.printStackTrace(); } return dbconn; } |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > html data extraction and insert to db |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|