要求:
做个表模型,例如:表有N行M列,程序启动时让其读取数据库中具有与其相同行列的某表.让后可以通过程序对数据库进行各种操作!!! ----谢谢,您的弹手几指,是对我莫大的帮助!!!

解决方案 »

  1.   

    import java.sql.*;
    public class DatabaseBasicOperation
    {
    private Statement sta; //用于数据库操作的Statement
    public static void main(String args[])
    {
    try
    {
    JDBCODBCBridge bridge=new JDBCODBCBridge();
    DatabaseBasicOperation operator=new DatabaseBasicOperation();
    ResultSet rs; //结果集
    String sqlCommand; //用于操作的SQL命令
    bridge.setURL("jdbc:odbc:sample");
    bridge.setUser("sa");
    bridge.setPassword("sa");
    Connection con=bridge.getConnection();
    operator.setStatement(con); //设置Satement
    //查询操作

    sqlCommand="SELECT * FROM STUDENTS";
    System.out.println("\n#########查询操作#########");
    System.out.println("输入SQL命令"+sqlCommand+"\n");
    rs=operator.executeQuery(sqlCommand);
    rs.next();
    for(int i=0;i<rs.getRow();i++)
    {
    System.out.println(rs.getString("NAME"));
    System.out.println(rs.getShort("age"));
    rs.next();
    }

    //修改操作

    sqlCommand=
    "UPDATE STUDENTS SET AGE=22 WHERE NAME=\'巧玲\'";
    System.out.println("\n#########修改操作#########");
    System.out.println("输入SQL命令"+sqlCommand+"\n");
    operator.executeUpdate(sqlCommand);
    sqlCommand="SELECT * FROM STUDENTS WHERE NAME=\'巧玲\'";
    rs=operator.executeQuery(sqlCommand);
    rs.next();
    for(int i=0;i<rs.getRow();i++)
    {
    System.out.println(rs.getString("NAME")+
    "年龄修改为"+rs.getInt("AGE"));
    rs.next();
    }

    //添加操作

    sqlCommand="INSERT INTO STUDENTS(ID,NAME,AGE)"+
    "VALUES(\'5\',\'小猫\',1)";
    System.out.println("\n#########添加操作#########");
    System.out.println("输入SQL命令"+sqlCommand+"\n");
    operator.executeInsert(sqlCommand);
    sqlCommand="SELECT * FROM STUDENTS WHERE ID=\'5\'";
    rs=operator.executeQuery(sqlCommand);
    rs.next();
    for(int i=0;i<rs.getRow();i++)
    {
    System.out.println("ID为"+rs.getString("ID")+
    "处已添加"+rs.getString("NAME"));
    rs.next();
    }
    //删除操作
    sqlCommand="DELETE FROM STUDENTS WHERE ID=\'5\'";
    System.out.println("\n#########删除操作#########");
    System.out.println("输入SQL命令"+sqlCommand+"\n");
    operator.executeDelete(sqlCommand);
    sqlCommand="SELECT * FROM STUDENTS WHERE ID=\'5\'";
    rs=operator.executeQuery(sqlCommand);
    rs.next();
    if(!rs.next())
    {
    System.out.println("不存在ID为5的数据,此数据已被删除");
    }
    //关闭所有连接,释放资源
    rs.close();
    operator.closeStatement();
    con.close();
    }
    catch(Exception e)
    {
    System.out.println(e.toString());
    }
    }
    public void setStatement(Connection con)
    {
    try
    {
    this.sta=con.createStatement(); //Statement中有关数据库查询的函数
    }
    catch(Exception e)
    {
    System.out.println(e.toString());
    }
    }
    public ResultSet executeQuery(String sqlCommand)
    {
    try
    {
    //Statement中有关修改数据库的函数
    return sta.executeQuery(sqlCommand); 
    }
    catch(Exception e)
    {
    System.out.println(e.toString());
    }
    return null;
    }
    public void executeUpdate(String sqlCommand)
    {
    try
    {
    sta.executeUpdate(sqlCommand);
    }
    catch(Exception e)
    {
    System.out.println(e.toString());
    }
    }
    public void executeInsert(String sqlCommand)
    {
    try
    {
    sta.executeUpdate(sqlCommand);
    }
    catch(Exception e)
    {
    System.out.println(e.toString());
    }

    public void executeDelete(String sqlCommand)
    {
    try
    {
    sta.executeUpdate(sqlCommand);
    }
    catch(Exception e)
    {
    System.out.println(e.toString());
    }
    }  
    public void closeStatement()
    {
    try
    {
    sta.close();

    }
    catch(Exception e)
    {
    System.out.println(e.toString());
    }
    }
    }
      

  2.   

    有谁能帮我看看这段代码为什么不能修改数据库里的数据!!!   
      private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                                try{    
                Connection con = DBControl.connectToDb();
                Statement stmt = con.createStatement();
               // int a=this.jTable2.getValueAt(jTable2.getSelectedRow(),0));//
               
               stmt.executeUpdate("exec UPDATE studentmaset("+this.jTextField4.getText()+","+this.jTextField5.getText()+", "+this.jTextField6.getText()+","+this.jTextField1.getText()+"),"+this.jTable2.getValueAt(jTable2.getSelectedRow(),0));
               // System.out.println("程序运行到此处!");           
                this.getData();
                this.getNull();
                jOptionPane2.showMessageDialog(this,"修改成功!");            
            }catch(Exception E){
                jOptionPane2.showMessageDialog(this,E.getMessage());
                E.printStackTrace();
            } 
        }  ==================================================================================
    语句
    stmt.executeUpdate("exec UPDATE studentmaset("+this.jTextField4.getText()+","+this.jTextField5.getText()+", "+this.jTextField6.getText()+","+this.jTextField1.getText()+"),"+this.jTable2.getValueAt(jTable2.getSelectedRow(),0));
     到底为什么出错?该怎么修改!