-
FEATURED COMPONENTS
First time here? Check out the FAQ!
index.zul :
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
<zk>
<style src="/styles.css" />
<window apply="org.zkoss.bind.BindComposer" id="win" use="data.controller" viewModel="viewmodel">
<grid width="270px">
<rows>
<row>
<label value="Name:"/>
<textbox id="nametxt" width="100px" sclass="my-input" />
</row>
<row>
<label value="Email:" />
<textbox id="emailtxt" width="100px" sclass="my-input" />
</row>
<row>
<button label="Submit" sclass="my-button" onClick="@command('tambahdata')"/>
</row>
</rows>
</grid>
<listbox id="lb" width="200" height="300" >
<listhead>
<listheader label="ID" sort="auto"/>
<listheader label="Nama"/>
<listheader label="email"/>
<listheader label="Address"/>
<listheader label="Phone"/>
<listheader label="Action"/>
</listhead>
<listitem forEach="${win.customers}" value="${each}">
<listcell label="${each.id}"/>
<listcell label="${each.nama}"/>
<listcell label="${each.email}"/>
<listcell label="${each.address}"/>
<listcell label="${each.phone}"/>
<listcell>
<button label="Edit" onClick="@command('edit', mahasiswa=mahasiswa)" />
<button label="Hapus" onClick="@command('hapus', mahasiswa=mahasiswa)" />
</listcell>
</listitem>
</listbox>
</window>
</zk>
viewmodel.java :
//package data;
package foo;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zul.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.zkoss.bind.annotation.Command;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import static sun.jvm.hotspot.HelloWorld.e;
public class viewmodel extends SelectorComposer<Window> {
@Wire
private Textbox nametxt;
@Wire
private Textbox emailtxt;
@Command
public void tambahdata() throws SQLException {
String name = nametxt.getValue();
String email = emailtxt.getValue();
try {
Connection connection = DriverManager.getConnection("jdbc:derby://localhost:1527/app");
PreparedStatement statement = connection.prepareStatement("INSERT INTO YourTable (name, email) VALUES (?, ?)");
statement.setString(1, name);
statement.setString(2, email);
statement.executeUpdate();
statement.close();
connection.close();
Messagebox.show("Data inserted successfully!");
}
catch (SQLException e) {
e.printStackTrace();
Messagebox.show("Error occurred while inserting data.");
}
}
}
After reading your code, I found you mix MVC and MVVM patterns together and contains some wrong usages. I suggest you reading Get ZK Up and Running with MVC which can help you understand basic concepts.
Asked: 2023-05-26 01:03:59 +0800
Seen: 3 times
Last updated: May 26