只需创建一个单独的类,并将与数据库的连接委托给他:
public class ConnectionManager { private static String url = "jdbc:mysql://localhost:3306/prototypeeop"; private static String driverName = "com.mysql.jdbc.Driver"; private static String username = "root"; private static String password = "triala"; private static Connection con; private static String urlstring; public static Connection getConnection() { try { Class.forName(driverName); try { con = DriverManager.getConnection(urlstring, username, password); } catch (SQLException ex) { // log an exception. fro example: System.out.println("Failed to create the database connection."); } } catch (ClassNotFoundException ex) { // log an exception. for example: System.out.println("Driver not found."); } return con; }}然后按以下代码获得连接:
private Connection con = null;private Statement stmt = null;private ResultSet rs = null;con = ConnectionManager.getConnection();stmt = con.createStatement();rs = stmt.executeQuery(sql);



