mysql数据库
SELECT id FROM good WHERE id<10;
id
1
2
3
4
5
6
7
8
9
SELECT GROUP_CONCAT(id) FROM good WHERE id<10;
1,2,3,4,5,6,7,8,9
进行了行转列
利用java的split对逗号进行分割,输出的就是数组。
方法二
查询出来存入list中,然后循环list,给数组赋值就行
import java.sql.*;
public class OracleObject {
public static final String DBDRIVER =
"oracle.jdbc.driver.OracleDriver";
public static final String DBURL =
"jdbc:oracle:thin:@10.196.127.185:1521:notebook";
public static final String DBUSER = "scott";
public static final String DBPASS = "zjx123";
public static void main(String args[]) throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select ID from STUDENT"; //查询学生学号
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
String[] arr=new String[10];
int i=0; //定义一个字符串数组
while(rs.next()) {
arr[i]=rs.getString(1);
i++; //把学生序号赋值给数组
}
for(j=0;j
} //打印数组
rs.close();
pstmt.close();
conn.close();
}
}
第一步:通过JDBC把数据库读取出来,放到List里
第二步:通过List接口里的toArray()转换为数组