日期:2014-05-20 浏览次数:20684 次
import javax.swing.*;
import java.sql.*;
class SqlDemo2 extends JFrame{
SqlDemo2(){
try{
String[] colHeads={"name","Phone","City"};
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:pubs";
Connection con=DriverManager.getConnection(url,"sa","");
Statement stmt=con.createStatement();
ResultSet rs;
rs=stmt.executeQuery("SELECT count(*) as au_count from authors");
rs.next();
int iCount=rs.getInt("au_count");
Object[][] data=new Object[iCount][];
int i=0;
rs=stmt.executeQuery("SELECT * from authors");
while(rs.next()){
data[i]=new Object[3];
data[i][0]=rs.getString("au_fname");
data[i][1]=rs.getString("Phone");
data[i][2]=rs.getString("City");
i++;
}
JTable table=new JTable(data,colHeads);
JScrollPane jsp=new JScrollPane(table);
this.getContentPane().add(jsp);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
catch(Exception e){
System.out.println(e);
}
}
public static void main(String[] args){
SqlDemo2 sd=new SqlDemo2();
sd.setSize(400,250);
sd.setVisible(true);
}
}