[Java]
Import java. SQL .*;
Import javax. SQL .*;
Public final class JdbcUtil {
/**
* @ Param args
*/
Private static String url = "jdbc: mysql: // localhost: 3306/jdbc"; // jdbc: mysql: // The local default jdbc port can be omitted.
Private static String user = "root"; www.2cto.com
Private static String password = "123456 ";
Private JdbcUtil (){}
Static {
Try {
Class. forName ("com. mysql. jdbc. Driver ");
} Catch (ClassNotFoundException e ){
Throw new ExceptionInInitializerError (e );
}
}
Public static Connection getConnection () throws SQLException {
Return DriverManager. getConnection (url, user, password );
}
Public static void free (ResultSet rs, Statement st, Connection conn ){
Try {
If (rs! = Null ){
Rs. close ();
}
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
Try {
If (st! = Null ){
St. close ();
}
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
If (conn! = Null ){
Try {
Conn. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
}
}
}
}
}
The static block is used to register the driver.
The getConnection function is used to obtain the connection.
The free function is used to safely release resources.