编译老出错,改得卡上的一个例子。
D:\computer\javaSystem\Tomcat\webapps\ROOT\WEB-INF\classes>javac te\UserManager.
java
te\UserManager.java:32: unreported exception java.lang.ClassNotFoundException; m
ust be caught or declared to be thrown
Connection conn1 = Te_conn.getConnect();
^
1 error
package te;/**
* <p>Title: jxc demo</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author cwx
* @version 1.0
*/import java.sql.*;
import java.util.*;
import te.db.Te_conn;public class UserManager {
public UserManager() {
} /**
* 处理用户登录
* @param strName 用户名称
* @param strPwd 密码
* @return 登录是否成功
*/
static public boolean login(String strName, String strPwd) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null; try {
conn = Te_conn.getConnect();
stmt = conn.prepareStatement("select * from te_user where V_username=?"); stmt.setString(1, strName);
rs = stmt.executeQuery();
if (rs.next()) {
String strPwd2 = rs.getString("V_pwd");
strPwd2.trim();
//检查密码
if (!strPwd2.equals(strPwd)) {
System.err.println(strName+"密码错误!");
return false;
} //验证通过
return true;
}
else {
System.err.println(strName+"用户不存在!");
return false;
}
}
catch (java.sql.SQLException e) {
System.err.println(e);
}
finally {
//关闭数据库资源
if (rs != null) {
try {
rs.close();
} catch (Exception exception) {}
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception exception) {}
}
if (conn != null) {
try {
conn.close();
} catch (Exception exception) {}
}
} return false;
}
}
D:\computer\javaSystem\Tomcat\webapps\ROOT\WEB-INF\classes>javac te\UserManager.
java
te\UserManager.java:32: unreported exception java.lang.ClassNotFoundException; m
ust be caught or declared to be thrown
Connection conn1 = Te_conn.getConnect();
^
1 error
package te;/**
* <p>Title: jxc demo</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author cwx
* @version 1.0
*/import java.sql.*;
import java.util.*;
import te.db.Te_conn;public class UserManager {
public UserManager() {
} /**
* 处理用户登录
* @param strName 用户名称
* @param strPwd 密码
* @return 登录是否成功
*/
static public boolean login(String strName, String strPwd) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null; try {
conn = Te_conn.getConnect();
stmt = conn.prepareStatement("select * from te_user where V_username=?"); stmt.setString(1, strName);
rs = stmt.executeQuery();
if (rs.next()) {
String strPwd2 = rs.getString("V_pwd");
strPwd2.trim();
//检查密码
if (!strPwd2.equals(strPwd)) {
System.err.println(strName+"密码错误!");
return false;
} //验证通过
return true;
}
else {
System.err.println(strName+"用户不存在!");
return false;
}
}
catch (java.sql.SQLException e) {
System.err.println(e);
}
finally {
//关闭数据库资源
if (rs != null) {
try {
rs.close();
} catch (Exception exception) {}
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception exception) {}
}
if (conn != null) {
try {
conn.close();
} catch (Exception exception) {}
}
} return false;
}
}
package te.db;import java.sql.*;
import java.util.*;
public class Te_conn { private static String error;
public static Connection con;
/* the modifier of con is public&static, so you can use it directly */ /* static {
try {
con = Te_conn.getConnect();
} catch (Exception e){
e.printStackTrace();
}
}*/ public Te_conn() { }
static public Connection getConnect() throws ClassNotFoundException,
SQLException,
Exception {
-------------------------------------------------------------------------
这个方法抛出的异常。getConnect() 里面是怎么写的?
SQLException,
Exception {
/* note: the modifier of getConnect() also is public&static */ Connection conn = null;
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://localhost/cnjbb2?useUnicode=true&characterEncoding=GBK","root","hantao"); } catch (ClassNotFoundException cnfe) {
error = "ClassNotFoundException: Could not locate DB driver.";
throw new ClassNotFoundException(error);
} catch (SQLException cnfe) {
error = "SQLException: Could not connect to database.";
throw new SQLException(error);
} catch (Exception e) {
error = "Exception: An unknown error occurred while connecting " +
"to database.";
throw new Exception(error);
}
return conn;
}
你必须对它做出相应的处理你调用getConnection()函数的函数必须对该异常处理,或捕获,或重新抛出
static public Connection getConnection2(){
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/cnjbb2?useUnicode=true&characterEncoding=GBK","root","dd");
return conn;
}
catch(java.lang.ClassNotFoundException e){
System.err.print("get connection error!");
System.err.print(e);
}
catch(java.sql.SQLException e){
System.err.print("get connection error!");
System.err.print(e);
} //出现错误,返回null
return null;
}D:\computer\javaSystem\Tomcat\webapps\ROOT\WEB-INF\classes>javac te\db\Te_conn.j
ava
te\db\Te_conn.java:81: unreported exception java.lang.InstantiationException; mu
st be caught or declared to be thrown
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
^
1 error如果要用进行捕获处理,或者重新抛出,应该在调用的时候怎么做?谢谢 蚂蚁yingtju
UserManager.java中声明如下,
static public boolean login (String strName, String strPwd)throws SQLException, Exception {
在SecurityServlet.java中调用USER时
if(UserManager.login (userName, password)){还是会出错,该如何处理,谢谢
不是应该这样吗?
public static boolean login (String ..............