-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi,
I'm using the following code to insert data into a MySQL database and it works fine.
I'd like to be able to separate the Java code (in the ZSCRIPT tags) into an external JSP file (eg to afterward centralized db connection...) and be able to call submit() method (with data to pass).
I reviewed docs, web and forums but I'm unable to find out how to achieve this.
Does anyone can help me please ?
Thanks
Tom
<?page title="new page title" contentType="text/html;charset=UTF-8"?> <zk> <window title="JDBC demo" border="normal"> <zscript><![CDATA[ import java.sql.*; void submit() { //load driver and get a database connetion Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password="); PreparedStatement stmt = null; try { stmt = conn.prepareStatement("INSERT INTO test.users (name,address) values(?, ?)"); //insert what end user entered into database table stmt.setString(1, name.value); stmt.setString(2, email.value); //execute the statement stmt.executeUpdate(); } finally { //cleanup if (stmt != null) { try { stmt.close(); } catch (SQLException ex) { log.error(ex); //log and ignore } } if (conn != null) { try { conn.close(); } catch (SQLException ex) { log.error(ex); //log and ignore } } } } ]]> </zscript> <vbox> <hbox>Name : <textbox id="name"/></hbox> <hbox>Email: <textbox id="email"/></hbox> <button label="submit" onClick="submit()"/> </vbox> </window> </zk>
ZK supports the MVC paradigm. You don't want that code in a JSP. JSP is view technology. Study up on MVC and how to use it with ZK.
Asked: 2011-07-09 13:24:42 +0800
Seen: 376 times
Last updated: Jul 10 '11