如何把database里的数据在AWT画面中显示?请举例?
请介绍有关JAVA数据库编程方面的书籍,谢谢!
------解决方案--------------------
给你个简单的例子,希望对你有启发.我觉得你看什么书无所谓,主要是你要花时间理解.
- Java code
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);
}
}
------解决方案--------------------
上边的例子 注意关闭连接