導航:首頁 > 數據分析 > java如何獲取資料庫指標

java如何獲取資料庫指標

發布時間:2023-10-08 01:03:59

java編程 如何獲取從資料庫中獲取的一條數據中一個一個數據 啊

看起來是這來句rs.getString("orderID")出錯,源
前面加上這句試試:
rs.next()

多條語句的話,可以這樣用:
while(rs.next())
{
job = rs.getString("orderID");
}

㈡ java中jatable連接資料庫步驟及其獲取數據方法

以下是一個小程序,參考下
import javax.swing.*;
import javax.swing.table.JTableHeader;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class Test extends JFrame{
// 定義組件
private JScrollPane scpDemo;
private JTableHeader jth;
private JTable tabDemo;
private JButton btnShow;
// 構造方法
public Test(){
// 窗體的相關屬性的定義
super("JTable數據綁定示例");
this.setSize(330,400);
this.setLayout(null);
this.setLocation(100,50);
// 創建組件
this.scpDemo = new JScrollPane();
this.scpDemo.setBounds(10,50,300,270);
this.btnShow = new JButton("顯示數據");
this.btnShow.setBounds(10,10,300,30);
// 給按鈕注冊監聽
this.btnShow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
btnShow_ActionPerformed(ae);
}
});
// 將組件加入到窗體中
add(this.scpDemo);
add(this.btnShow);
// 顯示窗體
this.setVisible(true);
}
// 點擊按鈕時的事件處理
public void btnShow_ActionPerformed(ActionEvent ae){
// 以下是連接數據源和顯示數據的具體處理方法,請注意下
try{
// 獲得連接
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:localServer","sa","");
// 建立查詢條件
String sql = "select * from localServer";
PreparedStatement pstm = conn.prepareStatement(sql);
// 執行查詢
ResultSet rs = pstm.executeQuery();
// 計算有多少條記錄
int count = 0;
while(rs.next()){
count++;
}
rs = pstm.executeQuery();
// 將查詢獲得的記錄數據,轉換成適合生成JTable的數據形式
Object[][] info = new Object[count][4];
count = 0;
while(rs.next()){
info[count][0] = Integer.valueOf( rs.getInt("id"));
info[count][1] = rs.getString("name");
info[count][2] = Integer.valueOf( rs.getInt("age") );
info[count][3] = rs.getString("sex");
count++;
}
// 定義表頭
String[] title = {"學號","姓名","年齡","性別"};
// 創建JTable
this.tabDemo = new JTable(info,title);
// 顯示表頭
this.jth = this.tabDemo.getTableHeader();
// 將JTable加入到帶滾動條的面板中
this.scpDemo.getViewport().add(tabDemo);
}catch(ClassNotFoundException cnfe){
JOptionPane.showMessageDialog(null,"數據源錯誤","錯誤",JOptionPane.ERROR_MESSAGE);
}catch(SQLException sqle){
JOptionPane.showMessageDialog(null,"數據操作錯誤","錯誤",JOptionPane.ERROR_MESSAGE);
}
}

public static void main(String[] args){
new Test();
}
}

㈢ Java獲取資料庫內容

如果不用加強for循環用for循環呢
然後輸出的是 list.get(i).getId()+","+list.get(i).getName()

㈣ Java如何獲取資料庫中的數據·案例

下面是一個從 mysql 數據獲取用戶數據的案例,可以參考一下:

importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjava.util.ArrayList;
importjava.util.List;

//用戶類,存儲單個用戶信息
classUser{

privateintid;

privateStringname;

publicUser(intid,Stringname){
this.id=id;
this.name=name;
}

publicintgetId(){
returnid;
}

publicvoidsetId(intid){
this.id=id;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}

@Override
publicStringtoString(){
return"User[id="+id+",name="+name+"]";
}
}

publicclassDemo1{

publicstaticvoidmain(String[]args)throwsClassNotFoundException,SQLException{

//本例使用mysql資料庫,演示將資料庫test的tb_users表中的用戶信息
//放到List中

//載入數據驅動
Class.forName("com.mysql.jdbc.Driver");

//資料庫連接字元串,此例資料庫為test
Stringurl="jdbc:mysql://localhost:3306/test";
Stringuser="root"; //資料庫用戶名
Stringpassword=""; //資料庫密碼

//打開一個數據連接
Connectionconn=DriverManager.getConnection(url,user,password);

Statementstmt=conn.createStatement();

//獲取表tb_users所有用戶信息到結果集中
ResultSetrs=stmt.executeQuery("SELECTid,nameFROMtb_users");

//定義一個存放用戶信息的List
List<User>users=newArrayList<>();

//提取用戶信息,並將用戶信息放入List
while(rs.next()){

//獲取用戶ID
intid=rs.getInt(1);

//獲取用戶名
Stringname=rs.getString(2);

users.add(newUser(id,name));
}

rs.close();
stmt.close();
conn.close();

//顯示用戶信息

for(Useru:users){
System.out.println(u);
}
}
}
閱讀全文

與java如何獲取資料庫指標相關的資料

熱點內容
win7zip文件怎麼打開 瀏覽:761
插在車里的u盤可以裝文件嗎 瀏覽:651
jspsession用法 瀏覽:383
java查看jvm快照 瀏覽:581
新文件夾1 瀏覽:412
眉山微網站報價多少 瀏覽:138
資料庫開發待遇什麼樣 瀏覽:721
cad文件打開工具欄圖標太大怎麼調整 瀏覽:52
蘋果安坤殺了小妹嗎 瀏覽:528
照片文件怎樣長久存放 瀏覽:816
g76沒退刀槽怎麼編程 瀏覽:749
5位qq貼吧 瀏覽:470
appinventor求時長 瀏覽:473
手機網路密碼忘了怎麼辦 瀏覽:945
太陽電池模擬模塊matlab程序 瀏覽:423
w7文件加密 瀏覽:749
網通密碼查看器 瀏覽:851
應用圖像和計算的實例教程 瀏覽:545
40級qq多少錢 瀏覽:488
表格如何自動生成數據 瀏覽:299

友情鏈接