请原谅我的英语不好。 我是java和SWT的新手。 我试图在SWT中显示树视图而不是在jface中。从数据库和i获取值应该显示树视图。
树视图就像,
模板 - 页码 - 布局编号 -
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class TreeEx
{
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/Tree";
static final String USER = "root";
static final String PASS = "root123";
public static void main(String[] args)
{
Connection conn = null;
Statement stmt = null;
int TemplateID=0;
int templateId =0 ;
int pageNumber = 0;
int PageNumber=0;
int layoutId = 0;
int LayoutId=0;
List<Integer> a1 = new ArrayList();
List a2 = new ArrayList();
List a3 = new ArrayList();
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql = "SELECT templateID,PageNumber,layoutid FROM templatepage, templatelayout WHERE templatelayout.pageid = templatepage.PageNumber";
String sql1="SELECT count(distinct templateID )TemplateID FROM templatepage, templatelayout WHERE templatelayout.pageid = templatepage.PageNumber";
String sql2="SELECT count(distinct PageNumber )PageNumber FROM templatepage, templatelayout WHERE templatelayout.pageid = templatepage.PageNumber";
String sql3="SELECT count(distinct layoutid )LayoutId FROM templatepage, templatelayout WHERE templatelayout.pageid = templatepage.PageNumber";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
{
templateId=rs.getInt("templateID");
pageNumber=rs.getInt("PageNumber");
layoutId=rs.getInt("layoutid");
a1.add(rs.getInt("templateID"));
a2.add(rs.getInt("PageNumber"));
a3.add(rs.getInt("layoutid"));
System.out.print("templateID: " + templateId);
System.out.print(", PageNumber: " + pageNumber);
System.out.print(", layoutid: " + layoutId+"\n");
System.out.println(a1);
}
rs.close();
ResultSet rs1 = stmt.executeQuery(sql1);
while(rs1.next())
{
TemplateID=rs1.getInt("TemplateID");
System.out.println("Distinct TemplateID"+TemplateID);
}
rs1.close();
ResultSet rs2 = stmt.executeQuery(sql2);
while(rs2.next())
{
PageNumber=rs2.getInt("PageNumber");
System.out.println("Distinct PageNumber"+PageNumber);
}
rs2.close();
ResultSet rs3 = stmt.executeQuery(sql3);
while(rs3.next())
{
LayoutId=rs3.getInt("LayoutId");
System.out.println("Distinct LayoutId"+LayoutId);
}
rs3.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if(stmt!=null)
conn.close();
}catch(SQLException se)
{
}
try
{
if(conn!=null)
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
}
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("SWT Trees");
shell.setLayout(new FillLayout());
shell.setSize(400, 300);
Tree tree = new Tree(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
for (int i = 0; i <TemplateID; i++)
{
TreeItem treeItem = new TreeItem(tree, 0);
treeItem.setText("template "+a1);
for (int j = 0; j <PageNumber; j++)
{
TreeItem subTreeItem = new TreeItem(treeItem, SWT.NONE);
subTreeItem.setText("pageNumber " +a2);
for (int k = 0; k <LayoutId; k++)
{
TreeItem subSubTreeItem = new TreeItem(subTreeItem, SWT.NONE);
subSubTreeItem.setText("layoutid "+a3);
System.out.println("**********"+layoutId);
}
}
}
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
我的代码显示为,
Temaple[1,1,1]
PageNumber[1,1,1,]
layoutid[1,2,3]
layoutid[1,2,3]
layoutid[1,2,3]
It should be
Template1
Pagenumber1
layoutid3
layoutid3
layoutid3
答案 0 :(得分:1)
确定。很难弄清楚问题,因为有一些不相关的代码。首先,请不要发送SQL查询,尝试对问题进行建模。但。如果是这样,请在finally {}块中调用close()方法。 ;)
问题是你有for循环,但你没有以正确的方式走在它上面。你循环三次,但你使用的值是相同的(完整的ArrayList)。你通过a1获得完整的数组; 使用get()方法到达实际元素或使用for-each循环!
treeItem.setText("template " + a1.get(i));