日期:2014-05-20  浏览次数:20857 次

一个关于javaSE连接数据库的问题
String[] selectServerString = null;;
String selectSql = "select distinct 姓名 from worker";

public void actionPerformed(ActionEvent arg0) {


Connection connection;
try {
connection = JdbcUtils.getConnection();
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(selectSql);
int i = 0;
while(result.next()){
selectServerString[i]=result.getString(1);
i++;

}

} catch (SQLException e) {
e.printStackTrace();
}

JComboBox selectServerBox = new JComboBox(selectServerString);

  }


但这报了空指针错误,是怎么回事,求帮助

------解决方案--------------------
Java code
"jdbc:sqlserver://localhost:1434;databaseName=binguan";

------解决方案--------------------
你的String数组没有实列化吧!
你好像写的是String selectServerString=null;没看到你在什么时方实例化啊!
要不你这样,在下面得到了ResultSet 写上selectServerString=new String[result.size];
 那后再把result里面的值放到数组里去:
public void actionPerformed(ActionEvent arg0) {


Connection connection;
try {
connection = JdbcUtils.getConnection();
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(selectSql);
int i = 0;
selectserverString=new String[result.size];//加上这个代码试试,()
while(result.next()){
selectServerString[i]=result.getString(1);
i++;

}

} catch (SQLException e) {
e.printStackTrace();
}

JComboBox selectServerBox = new JComboBox(selectServerString);

}

------解决方案--------------------
result.getString(1);
那你直接打印这里的值看看。