package gui;
import javax.swing.*;import java.io.*;
import java.sql.*;
import java.util.Vector;public class CategoryDA {
private static ResultSet rs=null;
private static Statement aStatement=null;
private static Connection aConnection=null;
static Vector Categories=new Vector();
private static void getDBConnection()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
aConnection=DriverManager.getConnection("jdbc:odbc:mydata","sa","sa");
}catch(ClassNotFoundException e1)
{
JOptionPane.showMessageDialog(null, "驱动没找到,连接失败","Error",JOptionPane.ERROR_MESSAGE);

}catch(SQLException e2)
{
JOptionPane.showMessageDialog(null, "用户名密码错误,连接失败","Error",JOptionPane.ERROR_MESSAGE);

}
}
public static Vector getAll()
{
Vector <Category> Categories=new Vector();
String strSQL;
strSQL="SELECT * FROM Categories";
try
{
getDBConnection();
aStatement=aConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=aStatement.executeQuery(strSQL);
while (rs.next())
{
Category aCategory=new Category();
aCategory.setCategoryID(rs.getInt("CategoryID"));
aCategory.setCategoryName(rs.getString("CategoryName"));
aCategory.setDescription(rs.getString("Description"));
byte [] buf1=rs.getBytes("Picture");
aCategory.setPicture(buf1);

Categories.add(aCategory);
}
rs.first();
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);

}
return Categories;
}
public static Category getCategory(ResultSet rs)
{
Category aCategory=new Category();
try
{
aCategory.setCategoryID(rs.getInt("CategoryID"));
aCategory.setCategoryName(rs.getString("CategoryName"));
aCategory.setDescription(rs.getString("Description"));
aCategory.setPicture(rs.getBytes("Picture"));
}
catch (SQLException e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, e.getMessage());
}
return aCategory;
}
public static Category GetCurrentCategory()
{
Category aCategory=new Category();
try
{
rs.absolute(rs.getRow());
aCategory=getCategory(rs);
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
return aCategory;
}
public static Category GetFirstCategory()
{
Category aCategory=new Category();
try
{
rs.first();
aCategory=getCategory(rs);

catch(SQLException e)
{
JOptionPane.showMessageDialog(null, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
return aCategory;
}
public static Category GetNextCategory()
{
Category aCategory=new Category();
try
{
if (rs.isLast())
{
JOptionPane.showMessageDialog(null, "已经是最后一条记录了","提示",JOptionPane.INFORMATION_MESSAGE);
rs.absolute(rs.getRow());
}
else
{
rs.next();

}
aCategory=getCategory(rs);
}catch(SQLException e)
{
JOptionPane.showMessageDialog(null, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);

}
return aCategory;
}
public static Category getPrevCategory()
{
Category aCategory=new Category();
try
{
if(rs.isFirst())
{
JOptionPane.showMessageDialog(null,"已经是第一条记录了","提示",JOptionPane.INFORMATION_MESSAGE);
rs.absolute(rs.getRow());
}
else
{
rs.previous();
}
aCategory=getCategory(rs);
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);

}
return aCategory;
}
public static Category GetLastCategory()
{
Category aCategory=new Category();
try
{
rs.last();
aCategory=getCategory(rs);
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);

}
return aCategory;
}
public static void closeConneciton()
{
try
{
if(rs!=null)
{
rs.close();
}
if(aStatement!=null)
{
aStatement.close();
}
if(aConnection!=null&&!aConnection.isClosed())
{
aConnection.close();
}

}catch(SQLException e) 
{
JOptionPane.showMessageDialog(null, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);

}

}
public static void update (Category aCategory)
{

try
{
rs.updateString("CategoryName", aCategory.getCategoryName());
rs.updateString("Description", aCategory.getDescription());
rs.updateRow();

}catch(SQLException e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
}
public static void updatePhoto(byte[] buf)
{

try
{
rs.updateBytes("Picture", buf);
rs.updateRow();
JOptionPane.showMessageDialog(null, "修改成功","提示",JOptionPane.INFORMATION_MESSAGE);


}catch(SQLException e)
{
JOptionPane.showMessageDialog(null, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);

}
}
public static void AddNew (Category aCategory)
{
try
{
rs.moveToInsertRow();
rs.updateString("CategoryName", aCategory.getCategoryName());
rs.updateString("Description", aCategory.getDescription());
rs.insertRow();
rs.last();
}catch(SQLException e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
public static void delete()
{
try
{
rs.deleteRow();
getAll();
}catch(SQLException e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
                       
}这是我的数据处理层的代码,当我插入数据或则更新数据的时候报出如下错误
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]SQL Server で特定できないエラーが発生しました。接続はサーバーにより切断された可能性があります。
at sun.jdbc.odbc.JdbcOdbcResultSet.setPos(JdbcOdbcResultSet.java:5272)
at sun.jdbc.odbc.JdbcOdbcResultSet.updateRow(JdbcOdbcResultSet.java:4172)
at gui.CategoryDA.update(CategoryDA.java:196)
at gui.CategoryFrame$btnUpdateActionEventHander.actionPerformed(CategoryFrame.java:189)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1766)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)从数据库取数据是没问题的,请教啊!!

解决方案 »

  1.   

    赫赫,你取数据库的时候,打开数据库连接,然后关闭掉.这个时候,rs的connection已经关闭了,你怎么可以更新呢?当然会告诉你,数据库已经关闭的错误了。
      

  2.   

        在getAll()方法中并没有看到手动调用关闭连接的操作,而且整个类中也只看到了closeConneciton()方法的定义,没有调用的说.这个类问题应该不大吧,也许是楼主在使用的时候出了问题吧.是不是使用时在更新之前手动调用了closeConneciton()方法.