gpt4 book ai didi

java - 从 java 类返回一个数组

转载 作者:行者123 更新时间:2023-11-30 06:26:26 24 4
gpt4 key购买 nike

我来自 UFT 背景。话说我对JAVA了解不多。我有以下代码,我连接到 sql server 并检索一些值并循环它们,然后将它们打印出来以记录。有一个字符串数组变量“String[] sqlArr”,它保存从 sql server 检索到的所有值。如何将“String[] sqlArr”变量返回到不同的类?在本例中是主类。

我不太确定如何返回数组,但我尝试将返回类型更改为 String 并在方法右大括号之前放置一个 return 语句。我收到编译错误。

我想知道的是我怎样才能,1. 不要在方法内循环它,而是将数组返回到 Main,这样我就可以根据需要使用数组的每个值。2. 在这种情况下,如果返回类型不是void,应该是什么?3. 也许有人可以推荐我或以不同的方式修改我的脚本,这样看起来会更专业。

任何对此的建议评论都将受到高度赞赏。

package com.mmia;

import java.sql.*;

public class Connect2SQLServer {

//Current Username
private String currentUser = System.getProperty("user.name");

public void connect2SQLServer() throws SQLException, ClassNotFoundException {

//Loading the required JDBC Driver class
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

//Creating a connection to the database
Connection conn = DriverManager.getConnection("jdbc:sqlserver://XXXXXXXXXXX;databaseName=Data_Table_Name;integratedSecurity=true");

//Executing SQL query and fetching the result
Statement st = conn.createStatement();

//Sql query
String sqlStr = "Select * From PropertiesTable where Username =" + "'" + currentUser + "'";

//Execute the query
ResultSet rs = st.executeQuery(sqlStr);

while (rs.next()) {

String Username = rs.getString("Username");
String Environment = rs.getString("Environment");
String WebDealer = rs.getString("WebDealer");
String WebAgent = rs.getString("WebAgent");
String WebPassword = rs.getString("WebPassword");
String InternalUser = rs.getString("InternalUser");
String InternalPassword = rs.getString("InternalPassword");
String Browser = rs.getString("Browser");

//String[] sqlArr;
String[] sqlArr = {Username, Environment, WebDealer, WebAgent, WebPassword, InternalUser, InternalPassword, Browser};

for (int i = 0; i < sqlArr.length; i++) {
System.out.println(sqlArr[i]);
}
}
}
}

最佳答案

可以为每个记录使用一个数组但是更好的方法是为每个记录创建一个对象并将所有这些记录收集到一个列表中。

示例:

class Record {

private String username;
private String environment;
private String webDealer;
private String webAgent;
private String webPassword;
private String internalUser;
private String internalPassword;
private String browser;

public Record(String username, String environment, String webDealer, String webAgent, String webPassword,
String internalUser, String internalPassword, String browser) {
this.username = username;
this.environment = environment;
this.webDealer = webDealer;
this.webAgent = webAgent;
this.webPassword = webPassword;
this.internalUser = internalUser;
this.internalPassword = internalPassword;
this.browser = browser;
}

public String getUsername() {
return username;
}

public String getEnvironment() {
return environment;
}

public String getWebDealer() {
return webDealer;
}

public String getWebAgent() {
return webAgent;
}

public String getWebPassword() {
return webPassword;
}

public String getInternalUser() {
return internalUser;
}

public String getInternalPassword() {
return internalPassword;
}

public String getBrowser() {
return browser;
}
}

然后你就可以使用它了:

// the method signature has changed and now it returns a list of records
public List<Record> connect2SQLServer() throws SQLException, ClassNotFoundException {
// ...


// create the list that will hold the results (records)
List<Record> results = new LinkedList<>();
while (rs.next()) {

String username = rs.getString("username");
String environment = rs.getString("environment");
String webDealer = rs.getString("webDealer");
String webAgent = rs.getString("webAgent");
String webPassword = rs.getString("webPassword");
String internalUser = rs.getString("internalUser");
String internalPassword = rs.getString("internalPassword");
String browser = rs.getString("browser");

// create a new record
Record record = new Record(username, environment, webDealer, webAgent,
webPassword, internalUser, internalPassword, browser);

// add the record to results
results.add(record);
}

return results;
}

评论:我冒昧地将你的变量名称修改为 . 以大写字母开头,以符合 Java 代码约定

关于java - 从 java 类返回一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47116017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com