jtable와 db연동에 관한 질문입니다.
율아
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.border.*;
import java.sql.*;
import javax.swing.table.*;public class Table_1 extends JFrame implements ActionListener
{
int pos2;
JButton jbt = new JButton(put);
String col_name[] = {Name,Jumin,tel,Addr};
String imsi2[][] = new String[pos2][4];
JTable jt= new JTable(imsi2, col_name);
JScrollPane jsp = new JScrollPane(jt);
Container con;
Connection conn;
PreparedStatement ps;
ResultSet rs;
Connection conn2;
PreparedStatement ps2;
ResultSet rs2;
JPanel jp = new JPanel(new BorderLayout());
public Table_1()
{
super(Table_1);
this.con_db();
this.start();
this.init();this.setSize(400,400);
this.setVisible(true);}
public void total_column()
{
String sqls2 = select count(*) from test2;
try{
ps2 = conn2.prepareStatement(sqls2);
rs2 = ps2.executeQuery();
while(rs2.next())
{
pos2 = rs2.getInt(1);
}
System.out.println(pos2:+ pos2);
}catch(SQLException e10)
{
System.out.println(e10 : + e10.getMessage() );
}
finally
{
try
{
rs2.close();
//System.out.println(total rs2가 종료 되었습니다);
ps2.close();
//System.out.println(total ps2가 종료 되었습니다);
}catch(Exception esks){}
}//finally
}
public void con_db()
{try
{
String url = jdbc:mysql://localhost/java;
String id = root;
String pw =1111;
Class.forName(org.gjt.mm.mysql.Driver);
conn = DriverManager.getConnection(url, id, pw);
conn2 = DriverManager.getConnection(url, id, pw);
System.out.println(succeed in connection for Database);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}catch(ClassNotFoundException not_class)
{
System.out.println(not_class : + not_class);
System.exit(-1);
}
catch(SQLException not_sql)
{
System.out.println(not_sql : + not_sql);
}
}
public void start()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void get_data()
{
String sqls = select * from test2;
try
{
ps = conn.prepareStatement(sqls);
rs = ps.executeQuery();
this.total_column(); //pos2의 값을 얻어내기 위한것
//pos2는 테이블의 인원수를 나타
imsi2 = new String[pos2][4];for(int i = 0; i pos2;)
{
while(rs.next())
{
imsi2[i][0] = rs.getString(1);
imsi2[i][1] = rs.getString(2);
imsi2[i][2] = rs.getString(3);
imsi2[i][3] = rs.getString(4);
i++;
}//while
}//for
this.invalidate();
this.validate();
this.repaint();}catch(SQLException e1)
{
System.out.println(e1: + e1);
}
}
public void init()
{
con = this.getContentPane();
con.setLayout(new BorderLayout());
jp.add(Center,jsp);
jp.add(South, jbt);
getContentPane().add(Center,jp);
}//init
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == jbt)
{
this.get_data();
}
}
public static void main(String[] args)
{
Table_1 t = new Table_1();
}
}
//========================================================
jtable에 창에 아무런 값도 뜨지를 않습니다..
다..
-
휘들램
쩝...
-
참없다
한말씀 드리자면.. Connection하는 부분과 데이타를 가져오는 부분.. 등은 다른 클래스로 만드시면
조금더 쉽게.. 기능을 정의하실수 있을듯합니다... -
파랑새
참고로, JDBC 드라이버를 로딩하는 코드 부분은 static 블럭에 넣어서, 클래스 로딩시 한 번만 로딩되도록 하는 것이 좋습니다. 매번 Connection을 생성할 때, 불필요하게 그 때마다 드라이버를 로딩할 필요는 없을테니까요. 드라이버를 로딩하는 작업은 시간 소모가 많은 부분입니다. 프로그램의 성능을 향상시키기 위한 Tip으로 알아두시는 것이 좋습니다.
-
내꼬야
이렇게 질문을 올리시면, 대략 난감합니다.............
질문의 포인트를 잘 정리해서........ 보는 사람이 쉽게 이해할 수 있도록 질문을 해야, 답변을 해주기도 쉽겠죠???