0
我必须添加项目jComboBox从数据库。 我正在使用Netbeans,不知道在哪个jComboBox事件中这个添加项目方法应该放置在 之内。如何添加项目到jcombobox
我必须添加项目jComboBox从数据库。 我正在使用Netbeans,不知道在哪个jComboBox事件中这个添加项目方法应该放置在 之内。如何添加项目到jcombobox
我不知道如果我完全理解你的问题,但我想你想是这样的:
String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
JComboBox box = new JComboBox(petStrings);
....
box.addItem("Mouse");
这会给你一个总体思路...
这里是一个样本函数我用SELECT语句来查询并得到一个结果集,并返回字符串的载体:
// Function to search the DB and return a vector containing the search
// results. The passed in value is a String used in a LIKE SQL search.
public Vector<String> Search(String query, int cid){
Vector<String> retVal = new Vector<String>();
try {
// A SQL statement
Statement st = DBObject.conn.createStatement();
// Get result set.
ResultSet r = st.executeQuery("SELECT filename FROM " +
"[P2P].[dbo].[FILE] WHERE filename LIKE '" + query + "' " +
"AND shared=1 AND client_ID!=" + cid + ";");
// Iterate through the results.
while(r.next()){
retVal.add(r.getString("filename"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retVal;
}
的DBOBJECT类:
abstract public class DBObject {
static Connection conn;
boolean valid;
// This tells us if we can use the object.
public boolean isValid() { return valid; }
}
如何连接到数据库:
// Set the connection string.
connectString = "jdbc:sqlserver://" + args[0] + ";user=P2P;password=1234;";
Mr.Bunk ...,我想知道在哪里尝试和添加项目组合框连接数据库的方法捕捉放置。 try和catch方法放置在jcomboboxactionactionperformed(...)或其他地方? – amish3110
Hi @ amish3110。 JComboBox.actionPerformed(ActionEvent e)方法不表示它会引发异常。你可以在原始问题中多发一点你的代码(编辑它),然后我可以帮你解决。我猜这应该是一个容易回答的问题......我需要更多地了解如何连接到数据库。 –
Mr.Bunk ...我有SQL服务器2005年。我有一个表名称数据,它具有slno,名称,类型,形状为列字段 我想显示名称,类型,形状的记录在组合框选择slno ...我有从数据库slno ..但编码显示从databse剩余的细节我不知道先生.. – amish3110