我必须从数据库向jComboBox添加项目。 我使用Netbeans并且不知道jComboBox在哪个事件中添加项目方法应该 放置。
答案 0 :(得分:0)
我不确定我是否完全理解你的问题,但我认为你想要这样的事情:
String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
JComboBox box = new JComboBox(petStrings);
....
box.addItem("Mouse");
这会给你一个大概的想法......
这是一个示例函数,我用它来查询SELECT语句并获取结果集并返回一个字符串Vector:
// 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;";