使用 Java 连接数据库值并添加到数组
Joining database values and add to an array using Java
这是我在 netbeans 中的 web 服务 java 项目的一部分。
我不知道如何从我的数据库中检索 two 值(描述 + culture - 这些是 columns), add 它们都到 array,然后检索它们两者。
我知道如何在仅检索 one 值时执行此操作。
public String countries(@WebParam(name = "name") String name) {
//TODO write your implementation code here
try {
String url = "jdbc:odbc:" + "worldcup";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(url,"","");
// Gets a statement
Statement state1 = con.createStatement();
//Statement state2 = con.createStatement();
String query1 = "SELECT description,culture FROM Countries WHERE name = '" + name + "'";
// selects the description for the selected group ( group will be referenced to the chosen group)
ResultSet results = state1.executeQuery(query1);
int i = 0;
Arrays.fill(names, "");
while (results.next()) {
String nam = results.getString("description"); // <---- this is the part i need help from
names[i++] = nam;
}
return Arrays.toString(names);
} catch (SQLException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
}
return null;
您可以通过列名或列索引访问该值。
按列名访问。
names[i++] = results.getString("description");
names[i++] = results.getString("culture");
按列索引访问。
names[i++] = results.getString(1);
names[i++] = results.getString(2);
这是我在 netbeans 中的 web 服务 java 项目的一部分。 我不知道如何从我的数据库中检索 two 值(描述 + culture - 这些是 columns), add 它们都到 array,然后检索它们两者。 我知道如何在仅检索 one 值时执行此操作。
public String countries(@WebParam(name = "name") String name) {
//TODO write your implementation code here
try {
String url = "jdbc:odbc:" + "worldcup";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(url,"","");
// Gets a statement
Statement state1 = con.createStatement();
//Statement state2 = con.createStatement();
String query1 = "SELECT description,culture FROM Countries WHERE name = '" + name + "'";
// selects the description for the selected group ( group will be referenced to the chosen group)
ResultSet results = state1.executeQuery(query1);
int i = 0;
Arrays.fill(names, "");
while (results.next()) {
String nam = results.getString("description"); // <---- this is the part i need help from
names[i++] = nam;
}
return Arrays.toString(names);
} catch (SQLException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
}
return null;
您可以通过列名或列索引访问该值。
按列名访问。
names[i++] = results.getString("description");
names[i++] = results.getString("culture");
按列索引访问。
names[i++] = results.getString(1);
names[i++] = results.getString(2);