在连接数据库时,如何将SQLException写到日志文件中去?

小弟初来咋到,多多指教。我菜鸟一个。如何将下列的JDBC写得更加健壮一些。我想将SQLException写到日志中去,异常跟踪。谢谢
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestMysql {

private static Connection conn = null;
private static Statement stmt = null;
private static ResultSet rs = null;
public static void main(String[] args) {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost/test","root","root");
        stmt = conn.createStatement();
        String sql = "select * from login";
        rs = stmt.executeQuery(sql);
        while(rs.next()){
            System.out.print(rs.getString(1)+"  ");
            System.out.print(rs.getString(2)+"  ");
            System.out.println(rs.getString(3));
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();//写入日志log中
    }finally{
        try {
                if(rs!=null){
                rs.close();
                rs = null;
                }
                if(stmt != null){
                    stmt.close();
                    stmt = null;
                }
                if(conn != null){
                    conn.close();
                    conn = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();//异常跟踪
            }

    }

}

}

log4j,这方面可以去网上查查