public class Main extends JFrame{
..........
class maketable
{
ResultSet rs;
ResultSetMetaData rsmd;
int rscol=0;
Vector rsv=new Vector();
请问,这个地方为什么一定要加上 ClassNotFoundException, InstantiationException, IllegalAccessException呢?我在内部不是已经有了try...catch了吗?
public maketable() throws ClassNotFoundException, InstantiationException, IllegalAccessException
{
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
Connection con=null;
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=123";
String user="sa";
String password="";
con=java.sql.DriverManager.getConnection(url,user,password);
Statement stmt=con.createStatement();
String sql="select * FROM testinfo";
rs=stmt.executeQuery(sql);
rsmd=rs.getMetaData();
rscol=rsmd.getColumnCount();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public Vector hod() throws SQLException //这里为什么又加上SQLException呢?
{
Vector headv=new Vector();
for(int i=1;i<rsmd.getColumnCount();i++)
{
headv.add(rsmd.getColumnName(i));
}
return headv;
}
public Main() {
jb.addActionListener(new ActionListener(){//为什么这里又不使用throws ClassNotFoundException, InstantiationException, IllegalAccessException呢 public void actionPerformed(ActionEvent event) {
javax.swing.table.DefaultTableModel dtm = null;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
Connection con=null;
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=123";
String user="sa";
String password="";
con=java.sql.DriverManager.getConnection(url,user,password);
Statement stmt=con.createStatement();
String sql="insert into testinfo values('"+jtf1.getText().toString()+"','"+jtf2.getText().toString()+"','"+jtf3.getText().toString()+"')";
stmt.executeUpdate(sql);
//System.out.print(sql);
}
catch(Exception ex)
{
System.err.println("catch了"+ex.getMessage());
}
maketable mk = null;
try {
mk = new maketable();//而这里做实例化,却还要使用try...catch呢??? } catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
}
try {
dtm = new DefaultTableModel(mk.rowod(), mk.hod());
} catch (SQLException ex) {
ex.printStackTrace();
}
JTable jt=new JTable(dtm);
jspn.setViewportView(jt);
}
}); }
}
..........
class maketable
{
ResultSet rs;
ResultSetMetaData rsmd;
int rscol=0;
Vector rsv=new Vector();
请问,这个地方为什么一定要加上 ClassNotFoundException, InstantiationException, IllegalAccessException呢?我在内部不是已经有了try...catch了吗?
public maketable() throws ClassNotFoundException, InstantiationException, IllegalAccessException
{
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
Connection con=null;
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=123";
String user="sa";
String password="";
con=java.sql.DriverManager.getConnection(url,user,password);
Statement stmt=con.createStatement();
String sql="select * FROM testinfo";
rs=stmt.executeQuery(sql);
rsmd=rs.getMetaData();
rscol=rsmd.getColumnCount();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public Vector hod() throws SQLException //这里为什么又加上SQLException呢?
{
Vector headv=new Vector();
for(int i=1;i<rsmd.getColumnCount();i++)
{
headv.add(rsmd.getColumnName(i));
}
return headv;
}
public Main() {
jb.addActionListener(new ActionListener(){//为什么这里又不使用throws ClassNotFoundException, InstantiationException, IllegalAccessException呢 public void actionPerformed(ActionEvent event) {
javax.swing.table.DefaultTableModel dtm = null;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
Connection con=null;
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=123";
String user="sa";
String password="";
con=java.sql.DriverManager.getConnection(url,user,password);
Statement stmt=con.createStatement();
String sql="insert into testinfo values('"+jtf1.getText().toString()+"','"+jtf2.getText().toString()+"','"+jtf3.getText().toString()+"')";
stmt.executeUpdate(sql);
//System.out.print(sql);
}
catch(Exception ex)
{
System.err.println("catch了"+ex.getMessage());
}
maketable mk = null;
try {
mk = new maketable();//而这里做实例化,却还要使用try...catch呢??? } catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
}
try {
dtm = new DefaultTableModel(mk.rowod(), mk.hod());
} catch (SQLException ex) {
ex.printStackTrace();
}
JTable jt=new JTable(dtm);
jspn.setViewportView(jt);
}
}); }
}
楼主【lhdjk】截止到2008-06-25 11:32:41的历史汇总数据(不包括此帖):
发帖数:22 发帖分:1070
结贴数:20 结贴分:890
未结数:2 未结分:180
结贴率:90.91 % 结分率:83.18 %
值得尊敬
我在内部不是已经有了try...catch了吗?
你的try 的是sqlException 不是 ClassNotFoundException, InstantiationException, IllegalAccessException
catch(Exception ex) //Exception是就包括了所有的exception
第二个问题:rsmd.getColumnName会产生SQLException ,你可以用try catch来捕获这个例外,
throws-throw
都是基本语法 有规定的用法 没有为什么
数据库操作和io操作一般是要求捕获异常的,要么try,要么方法加exception
用不同的抛出 比较方法找出不同的错误。而不是仅仅一个综合的Exception