0

Access Database and Selecting items

asked 2009-11-27 10:06:40 +0800

JoH gravatar image JoH
105 2 3

Dear all,

I'm new to java prgramming.

I created a database named lmudb with two accounts.
One for the administrator, who has the ability to modify the database.
And the other one is only for the guest, who has the ability to search the database.

The .zul file below is the window for the guest.
My Problem is, I could display the content of the database instantly by implementing the method I created in another class, and also select the the filedir by the method select(), and show the file directory in the message box by clicking the button test (shown in the <west></west> area).


But then, if the user search or filter a string, let's say "cysteine", the new list will be showed. And if one try to click the button test again using the method select(), it doesn't work. It seems the new list cannot be found by the method select().
The error message I got:
Attempt to invoke method getFiledir on null value

How could I make that the method select still invokable while a new list is being created?

Thanks for every reply,

-Johnny-

<?page id="UserZul" title=" New ZUL Title" cacheable="false"
language="xul/html" zscriptLanguage="Java" contentType="text/html;charset=UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit"?>

<zk xmlns="http://www.zkoss.org/2005/zul"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd">


<window id="guestWnd" title="Database Search" border="normal" width="1422px" use="lmu.qc.ui.PeopleWindow">
<zscript >
public void select(){ Listitem li = items.getSelectedItem();
if(li!=null){ String filedir =
((lmu.qc.domain.Item)li.getValue()).getFiledir();
Messagebox.show("Are you sure of show " + filedir); } }
</zscript>

<zscript language="Java">{
import java.sql.*;

public void submit1() {

Connection conn = null;
try
{
String userName = "root";
String password = "compound";
String url = "jdbc:mysql://localhost:3306/lmudb";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");

Statement s = conn.createStatement ();
s.executeQuery ("SELECT * FROM items WHERE strform like '%"+ strform.value+"%'");
ResultSet rs = s.getResultSet ();

while (items.getItemCount() > 0) {
items.removeItemAt(0);
}
while (rs.next())
{
Listitem li = new Listitem();

li.appendChild(new Listcell(rs.getString("strform")));
li.appendChild(new Listcell(rs.getString("filedir")));
li.appendChild(new Listcell(rs.getString("description")));
li.appendChild(new Listcell(rs.getString("qcmeth")));
li.appendChild(new Listcell(rs.getString("pg")));
li.appendChild(new Listcell(rs.getString("es")));
li.appendChild(new Listcell(rs.getString("multi")));
li.appendChild(new Listcell(rs.getString("charge")));
li.appendChild(new Listcell(rs.getString("energy")));
items.appendChild(li);
}
rs.close ();
s.close ();
}
catch (Exception e)
{
System.err.println ("ERROR: "+ e.getMessage());
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}


}



public void submit2() {

Connection conn = null;
try
{
String userName = "root";
String password = "compound";
String url = "jdbc:mysql://localhost:3306/lmudb";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");

Statement s = conn.createStatement ();
s.executeQuery ("SELECT * FROM items WHERE description like '%"+ description.value+"%'");
ResultSet rs = s.getResultSet ();

while (items.getItemCount() > 0) {
items.removeItemAt(0);
}
while (rs.next())
{
Listitem li = new Listitem();

li.appendChild(new Listcell(rs.getString("filedir")));
li.appendChild(new Listcell(rs.getString("strform")));
li.appendChild(new Listcell(rs.getString("description")));
li.appendChild(new Listcell(rs.getString("qcmeth")));
li.appendChild(new Listcell(rs.getString("pg")));
li.appendChild(new Listcell(rs.getString("es")));
li.appendChild(new Listcell(rs.getString("multi")));
li.appendChild(new Listcell(rs.getString("charge")));
li.appendChild(new Listcell(rs.getString("energy")));
items.appendChild(li);
}

rs.close ();
s.close ();
}
catch (Exception e)
{
System.err.println ("ERROR: "+ e.getMessage());
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}


}





}</zscript>


<grid>
<rows>
<row>Structure Formula: <textbox width="99%" id="strform" /><button id="search1" label="Search by Structure Formula" onClick="submit1()"/></row>
<row>Description: <textbox id="description" width="99%" /><button id="search2" label="Search by Description" onClick="submit2()"/></row>
</rows>
</grid>
<borderlayout height="1000px"><west title="Result List" width="1012px" splittable="true" border="none" flex="true" collapsible="true">
<listbox id="items" width="1012px" mold="paging" onCreate="guestWnd.showList()">
<listhead>
<listheader width="100px" label="File Path" />
<listheader sort="auto" width="120px" label="Structure Formula" />
<listheader sort="auto" width="100px" label="Description" />
<listheader width="120px" label="Methods" />
<listheader width="50px" label="PG" />
<listheader width="50px" label="ES" />
<listheader width="80px" label="Multiplicity" />
<listheader width="100px" label="Charge" />
<listheader sort="auto" width="120px" label="Energy" />
</listhead>
</listbox>
</west>
<east title="Jmol Display" width="390px" collapsible="true" flex="true">
<button label="test" onClick="select()" />
</east>
</borderlayout>
</window>
</zk>

delete flag offensive retag edit

1 Reply

Sort by ยป oldest newest

answered 2009-11-29 19:47:36 +0800

joylo0122 gravatar image joylo0122
688 1
www.zkoss.org

@JoH

Hi Joh, maybe you should use System.out.println to check where the correct problem happened.
For example, not only read error messages, you must find out which value is null and check your
programming logic. There is no use to post whole of the codes, it doesn't help.

/Joy

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2009-11-27 10:06:40 +0800

Seen: 761 times

Last updated: Nov 29 '09

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More