日期:2014-05-20  浏览次数:20952 次

数据库读取问题!!!!!!!!!!!!!!(高手来啊)
import   java.sql.Connection;
import   java.sql.ResultSet;
import   java.sql.Statement;

import   javax.swing.JButton;
import   javax.swing.JFrame;
import   javax.swing.JTable;
import   java.sql.*;


public   class   MainFrame   extends   JFrame   {

private   JTable   table;

public   static   Connection   conn;
public   static   Statement   stmtBank;
public   static   ResultSet   rsBank;


String   DBDriver= "org.hsqldb.jdbcDriver ";
String   DBSource= "jdbc:hsqldb:d:\\mydb ";
String   DBUser= "sa ";
String   pwd= " ";

String   sSql;
/**
  *   Launch   the   application
  *   @param   args
  */
public   static   void   main(String   args[])   {
try   {
MainFrame   frame   =   new   MainFrame();
frame.setVisible(true);
}   catch   (Exception   e)   {
e.printStackTrace();
}
}

/**
  *   Create   the   frame
  */
public   MainFrame()   {
super();

try{
Class.forName(DBDriver);
conn=DriverManager.getConnection(DBSource,   DBUser,   pwd);
}catch(SQLException   ex){
ex.printStackTrace();
}catch(ClassNotFoundException   ex){
ex.printStackTrace();
}

try{
stmtBank=conn.createStatement();
}catch(Exception   ex){
ex.printStackTrace();
}

sSql= "select   *   from   bankacnt ";

try{
rsBank=stmtBank.executeQuery(sSql);
}catch(SQLException   ex){
ex.printStackTrace();
}


getContentPane().setLayout(null);
setBounds(100,   100,   500,   375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

table   =   new   JTable();
table.setBounds(10,   10,   472,   129);
getContentPane().add(table);

final   JButton   btnok   =   new   JButton();
btnok.setText( "新   增 ");
btnok.setBounds(40,   269,   63,   25);
getContentPane().add(btnok);

String   Content[][]=new   String[0][6];
String[]   Header={ "col1 ", "col2 ", "col3 ", "col4 "};

int   rowNum=0;
int   total=0;
try{
rsBank.afterLast();
if(rsBank.previous())total   =   rsBank.getRow();
//Move   back   to   the   first   record;  
rsBank.beforeFirst();  

if   (total!=0){

Content   =   new   String[total][4];
while(rsBank.next())
{
Content[rowNum][0]   =   " "   +   rsBank.getString( "acntNumber ");
Content[rowNum][1]   =   " "   +   rsBank.getString( "name ");
Content[rowNum][2]   =   " "   +   rsBank.getString( "companyname ");
Content[rowNum][3]   = " "+     rsBank.getDouble( "branchID ");
rowNum++;
}
}
else
{
Content   =   new   String[0][4];
Content[0][0]   =   "   ";