0
MySQL 데이터베이스에 성공적으로 연결되어 있고 명령에서 데이터를 인쇄 할 수 있지만 JTable에 배치하려고합니다. 다음은 허용되지 않지만 어떻게 할 수 있는지 알고 있습니다.JTable 넷빈을 채우기 위해 데이터를 얻으십시오
jTable1.setValueAt(connect.rs.getString("name"), row, 0);
jTable1.setValueAt(connect.rs.getString("price"), row, 1);
올바른 방법은 다음과 같습니다.
jTable1.setValueAt(connect.rs.getString(1), row, 0);
jTable1.setValueAt(connect.rs.getString(2), row, 1);
그러나 1과 2가 무엇을 나타내는지는 알 수 없습니다.
정말 모든 데이터를 가져 와서 MySQL의 이름과 가격 변수로 JTable을 채우기 만하면됩니다.
여기 내 DBConnect 클래스입니다. 이것은 당신이 데이터베이스에서 데이터를 얻을하는 데 도움이 될 것입니다
data = get();
//create header for the table
header = new Vector<String>();
header.add("Column1");
header.add("Column2");
...
model=new DefaultTableModel(data,header);
table = new JTable(model);
JTable의
에import java.sql.*;
public class DBConnect {
private Connection con;
private Statement st;
public ResultSet rs;
public String name;
public String ph;
public DBConnect(){
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://buiud.com:3306/buiud458_androidhive","XXX","XXX");
st = con.createStatement();
}
catch(Exception ex){
System.out.println("Err: "+ex);
}
}
public void getData(){
try{
String query = "select * from products";
rs = st.executeQuery(query);
System.out.println("Records from database");
while(rs.next()){
name = rs.getString("name");
ph = rs.getString("price");
System.out.println("Name: "+name+" "+"Price: "+ph);
}
}catch(Exception ex){
System.out.println("ERR: "+ex);
}
}
}
직접 찍은 다음 [질문]보십시오 (http://stackoverflow.com/questions/877 5076/jtablejdbc-easiest-way)? – kshikama