本人编写了一个简单的DB类,语法错误已经消除了,编译的时候无法通过,原因似乎是异常处理的语句没有些造成的,类的详细代码如下:
同时关于异常处理本人也有些其它疑问:
1、异常处理是不是必须每个方法里都要写;
2、异常处理是不是可以集中起来处理交给一个类去处理,而不用在每个方法里面写,可以大话要怎么办;import java.net.URL;
import java.sql.*;
public class DB
{
String DriverName="com.microsoft.jdbc.sqlserver.SQLServerDriver"; String DBHost="";
String DBName="";
String UserName="";
String Password="";

    Connection connObj;
    ResultSet rsObj;
Statement stmtObj;
DatabaseMetaData dbmdObj;//*******************************************************************************
public boolean InitDBDriver(String DNString)
{
try
{
if(DNString!="")
{
Class.forName(DNString);
}
else
{
Class.forName(this.DriverName);
}
return true;
}
        catch(ClassNotFoundException e)
{
            // Could not find the database driver
            System.out.println("Could not find the database driver!");
        }
}
public boolean InitDBInfo(String DBHost,String DBName,String UserName,String Password)
{
this.DBHost=DBHost;
this.DBName=DBName;
this.UserName=UserName;
this.Password=Password;
return true;
}
//*******************************************************************************
private boolean getConn()
{
try
{
String url="jdbc:microsoft:sqlserver://"+ DBHost +";DatabaseName="+DBName;
connObj = DriverManager.getConnection(url,this.UserName,this.Password);
return true;
}
        catch(SQLException e)
{
            // Could not connect to the database
            System.out.println("Could not connect to the database!");
            System.err.println(e.getMessage());
        }
} public boolean ConnOpen()
{
getConn();
stmtObj = connObj.createStatement();
return true;
}
public boolean ConnClose()
{
connObj.close();
return true;
}//*******************************************************************************
public boolean SQLUpdate(String SQLString)
{
        try {
            stmtObj.executeUpdate(SQLString);
            connObj.commit();
            return true;        }
        catch (SQLException sqle) {
            System.out.println(sqle.toString());
            return false;
        }
} public ResultSet SQLQuery(String SQLString)
{
        try {
            rsObj = stmtObj.executeQuery(SQLString);
            return rsObj;
        }
        catch (SQLException sqle) {
            System.out.println(sqle.toString());
            return null;
        }
}//*******************************************************************************
public boolean DBinfoPrint()
{
DatabaseMetaData dbmdObj=connObj.getMetaData();
System.out.println("\nConnected to"+dbmdObj.getURL()+"\n");
System.out.println("Driver "+dbmdObj.getDriverName()+"\n");
System.out.println("Version "+dbmdObj.getDriverVersion()+"\n");
return true;
}
//*******************************************************************************
public void main(String args[]) throws Exception
{
DB testdb1;
testdb1=new DB();
testdb1.InitDBInfo("localhost","pubs","sa","");
testdb1.ConnOpen();
testdb1.DBinfoPrint();
testdb1.SQLUpdate("insert into [jobs] (job_desc,min_lvl,max_lvl)values('abc',25,99)");
testdb1.ConnClose();
}
}
编译结果如下:
DB.java:66: unreported exception java.sql.SQLException; must be caught or declar
ed to be thrown
                stmtObj = connObj.createStatement();
                                 ^
DB.java:71: unreported exception java.sql.SQLException; must be caught or declar
ed to be thrown
                connObj.close();
                       ^
DB.java:105: unreported exception java.sql.SQLException; must be caught or decla
red to be thrown
                DatabaseMetaData dbmdObj=connObj.getMetaData();
                                                ^
DB.java:106: unreported exception java.sql.SQLException; must be caught or decla
red to be thrown
                System.out.println("\nConnected to"+dbmdObj.getURL()+"\n");
                                                           ^
DB.java:107: unreported exception java.sql.SQLException; must be caught or decla
red to be thrown
                System.out.println("Driver "+dbmdObj.getDriverName()+"\n");
                                                    ^
DB.java:108: unreported exception java.sql.SQLException; must be caught or decla
red to be thrown
                System.out.println("Version "+dbmdObj.getDriverVersion()+"\n");
                                                     ^
8 errors