日期:2014-05-20 浏览次数:20964 次
private static ResultSet rs=null;
private static Statement aStatement=null;
private static Connection aConnection=null;
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 GetNextCategory()
{
Category aCategory=new Category();
try
{
if (rs.isLast())
{
JOptionPane.showMessageDialog(null, "已经是最后一条记录了","提示",JOptionPane.INFORMATION_MESSAGE);
}
else
{
rs.next();
}
aCategory=getCategory(rs);
}catch(SQLException e)
{
JOptionPane.showMessageDialog(null, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
return aCategory;
}
catch(SQLException e)
{
e.printStackTrace();//加这个就可以看到控制台里详细信息了
JOptionPane.showMessageDialog(null, e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}