java取數據
『壹』 java 從list中取出數據
要看你在 .select(sql, list); //把查出來的數據放到list中了
是怎樣保存進去的
一般用 list.get(i);來獲得那個保存數據的類
獲得具體的數據要list.get(i).getXX(); 在.select(sql, list); 中應該是用setXX()保存進去的
『貳』 怎樣用Java獲取內存中的數據
方法如下:
首先
創建一個Bean用來存貯要得到的信
public class MonitorInfoBean {
/** 可使用內存. */
private long totalMemory;
/** 剩餘內存. */
private long freeMemory;
/** 最大可使用內存. */
private long maxMemory;
/** 操作系統. */
private String osName;
/** 總的物理內存. */
private long totalMemorySize;
/** 剩餘的物理內存. */
private long freePhysicalMemorySize;
/** 已使用的物理內存. */
private long usedMemory;
/** 線程總數. */
private int totalThread;
/** cpu使用率. */
private double cpuRatio;
public long getFreeMemory() {
return freeMemory;
}
public void setFreeMemory(long freeMemory) {
this.freeMemory = freeMemory;
}
public long getFreePhysicalMemorySize() {
return freePhysicalMemorySize;
}
public void setFreePhysicalMemorySize(long freePhysicalMemorySize) {
this.freePhysicalMemorySize = freePhysicalMemorySize;
}
public long getMaxMemory() {
return maxMemory;
}
public void setMaxMemory(long maxMemory) {
this.maxMemory = maxMemory;
}
public String getOsName() {
return osName;
}
public void setOsName(String osName) {
this.osName = osName;
}
public long getTotalMemory() {
return totalMemory;
}
public void setTotalMemory(long totalMemory) {
this.totalMemory = totalMemory;
}
public long getTotalMemorySize() {
return totalMemorySize;
}
public void setTotalMemorySize(long totalMemorySize) {
this.totalMemorySize = totalMemorySize;
}
public int getTotalThread() {
return totalThread;
}
public void setTotalThread(int totalThread) {
this.totalThread = totalThread;
}
public long getUsedMemory() {
return usedMemory;
}
public void setUsedMemory(long usedMemory) {
this.usedMemory = usedMemory;
}
public double getCpuRatio() {
return cpuRatio;
}
public void setCpuRatio(double cpuRatio) {
this.cpuRatio = cpuRatio;
}
}
之後,建立bean的介面
public interface IMonitorService {
public MonitorInfoBean getMonitorInfoBean() throws Exception;
}
然後,就是最關鍵的,得到cpu的利用率,已用內存,可用內存,最大內存等信息。
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import sun.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
import java.io.*;
import java.util.StringTokenizer;
/**
* 獲取系統信息的業務邏輯實現類.
* @author GuoHuang
*/
public class MonitorServiceImpl implements IMonitorService {
private static final int CPUTIME = 30;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;
private static final File versionFile = new File("/proc/version");
private static String linuxVersion = null;
/**
* 獲得當前的監控對象.
* @return 返回構造好的監控對象
* @throws Exception
* @author GuoHuang
*/
public MonitorInfoBean getMonitorInfoBean() throws Exception {
int kb = 1024;
// 可使用內存
long totalMemory = Runtime.getRuntime().totalMemory() / kb;
// 剩餘內存
long freeMemory = Runtime.getRuntime().freeMemory() / kb;
// 最大可使用內存
long maxMemory = Runtime.getRuntime().maxMemory() / kb;
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
// 操作系統
String osName = System.getProperty("os.name");
// 總的物理內存
long totalMemorySize = osmxb.getTotalPhysicalMemorySize() / kb;
// 剩餘的物理內存
long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize() / kb;
// 已使用的物理內存
long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb
.getFreePhysicalMemorySize())
/ kb;
// 獲得線程總數
ThreadGroup parentThread;
for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
.getParent() != null; parentThread = parentThread.getParent())
;
int totalThread = parentThread.activeCount();
double cpuRatio = 0;
if (osName.toLowerCase().startsWith("windows")) {
cpuRatio = this.getCpuRatioForWindows();
}
else {
cpuRatio = this.getCpuRateForLinux();
}
// 構造返回對象
MonitorInfoBean infoBean = new MonitorInfoBean();
infoBean.setFreeMemory(freeMemory);
infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize);
infoBean.setMaxMemory(maxMemory);
infoBean.setOsName(osName);
infoBean.setTotalMemory(totalMemory);
infoBean.setTotalMemorySize(totalMemorySize);
infoBean.setTotalThread(totalThread);
infoBean.setUsedMemory(usedMemory);
infoBean.setCpuRatio(cpuRatio);
return infoBean;
}
private static double getCpuRateForLinux(){
InputStream is = null;
InputStreamReader isr = null;
BufferedReader brStat = null;
StringTokenizer tokenStat = null;
try{
System.out.println("Get usage rate of CUP , linux version: "+linuxVersion);
Process process = Runtime.getRuntime().exec("top -b -n 1");
is = process.getInputStream();
isr = new InputStreamReader(is);
brStat = new BufferedReader(isr);
if(linuxVersion.equals("2.4")){
brStat.readLine();
brStat.readLine();
brStat.readLine();
brStat.readLine();
tokenStat = new StringTokenizer(brStat.readLine());
tokenStat.nextToken();
tokenStat.nextToken();
String user = tokenStat.nextToken();
tokenStat.nextToken();
String system = tokenStat.nextToken();
tokenStat.nextToken();
String nice = tokenStat.nextToken();
System.out.println(user+" , "+system+" , "+nice);
user = user.substring(0,user.indexOf("%"));
system = system.substring(0,system.indexOf("%"));
nice = nice.substring(0,nice.indexOf("%"));
float userUsage = new Float(user).floatValue();
float systemUsage = new Float(system).floatValue();
float niceUsage = new Float(nice).floatValue();
return (userUsage+systemUsage+niceUsage)/100;
}else{
brStat.readLine();
brStat.readLine();
tokenStat = new StringTokenizer(brStat.readLine());
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
tokenStat.nextToken();
String cpuUsage = tokenStat.nextToken();
System.out.println("CPU idle : "+cpuUsage);
Float usage = new Float(cpuUsage.substring(0,cpuUsage.indexOf("%")));
return (1-usage.floatValue()/100);
}
} catch(IOException ioe){
System.out.println(ioe.getMessage());
freeResource(is, isr, brStat);
return 1;
} finally{
freeResource(is, isr, brStat);
}
}
『叄』 在java中如何取出資料庫表中某行的數據
limit
限制的是從第幾行開始,mysql
默認的有0行
,
limit
3,1;是從第四行開始,步長為1.希望能幫助你。
『肆』 java 取資料庫值
最好的方法就是使用類集list配上范型,例如List<Integer>
list=new
List<Integer>().然後使用for循環講pk值存入,while(rs.hasnext){list.add(rs.getInt(這里填PK值段的序列))},當然也可以不用范性,但編譯有安全警告
『伍』 Java中通過txt文件存儲和取出數據
Java中讀取txt文件可以使用file類先創建一個對象,然後使用I/O操作,進行讀取或者寫入操作,示例如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class demo2 {
private static String path = "f:/demo1.txt";
private static File file;
static{
file = new File(path);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
Student stu = new Student(1,"張三",90);
writeDataToFile(file,stu);
readDataFromFile(file);
}
private static void readDataFromFile(File file) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String str = "";
while((str = reader.readLine())!=null){
String[] stuInfo = str.split(",");
System.out.println("學號:"+stuInfo[0]+" 姓名:"+stuInfo[1]+" score:"+stuInfo[2]);
}
}
private static void writeDataToFile(File file,Student stu) throws FileNotFoundException {
PrintWriter out = new PrintWriter(new FileOutputStream(file, true));
out.println(stu.toString());
out.close();
}
}
『陸』 java取數據問題
可以看一下redis,用set數據類型能很好的解決你的問題。
『柒』 java 從字元串提取數據
可以通過java的」substring「方法截取出指定的字元串,前提是知道開始和結束的字元串的值:
String getSignInfo = reqResult.substring(reqResult.indexOf("(") +1, reqResult.indexOf(")"));
解釋:上述方法就是截取reqResult字元串的中開始」(「和結束」(「中間部分的內容,」1「就是」(「的長度,之後將獲取的結果賦值給」getSignInfo進行輸出即可「;
備註:以上方法通用於截取字元串,數字」1「和開始結束字元串根據實際需要修改即可。
『捌』 在java中怎麼取出數組中的數據
1.方法:
public final synchronized void setsize(int newsize);
此方法用來定義向量的大小,若向量對象現有成員個數已經超過了newsize的值,則超過部分的多餘元素會丟失。
2.程序中定義Enumeration類的一個對象Enumeration是java.util中的一個介面類,
在Enumeration中封裝了有關枚舉數據集合的方法。
在Enumeration提供了方法hasMoreElement()來判斷集合中是否還有其他元素和方法nextElement()來判斷集合中是否還有其他元素和方法nextElement()來獲取下一個元素。利用這兩個方法,可以依次獲得集合中的元素。
3.Vector中提供方法:
public final synchronized Enumeration elements();
此方法將向量對象對應到一個枚舉類型。java.util包中的其他類中也都有這類方法,以便於用戶獲取對應的枚舉類型。
『玖』 java!怎麼取數據啊
List list= //後面是你這個方法的名子
for(int i=0;i<list.size();i++){
String [] array=(String[])list.get(i);
System.out.print(array[0]);
System.out.print(array[0]);
System.out.print(array[0]);
System.out.print(array[0]);
}
『拾』 java中如何從文件中讀取數據
分為讀位元組,讀字元兩種讀法
◎◎◎FileInputStream 位元組輸入流讀文件◎◎◎
public class Maintest {
public static void main(String[] args) throws IOException {
File f=new File("G:\\just for fun\\xiangwei.txt");
FileInputStream fin=new FileInputStream(f);
byte[] bs=new byte[1024];
int count=0;
while((count=fin.read(bs))>0)
{
String str=new String(bs,0,count); //反復定義新變數:每一次都 重新定義新變數,接收新讀取的數據
System.out.println(str); //反復輸出新變數:每一次都 輸出重新定義的新變數
}
fin.close();
}
}
◎◎◎FileReader 字元輸入流讀文件◎◎◎
public class Maintest {
public static void main(String[] args) throws IOException {
File f=new File("H:\\just for fun\\xiangwei.txt");
FileReader fre=new FileReader(f);
BufferedReader bre=new BufferedReader(fre);
String str="";
while((str=bre.readLine())!=null) //●判斷最後一行不存在,為空
{
System.out.println(str);
}
bre.close();
fre.close();
}
}