首先导入jdbc所用的jar包
然后分别调用getCon()和closeCon方法
import java.sql.DriverManager; import java.sql.SQLException; import com.mysql.jdbc.Connection; public class DbUtil { private String dbUrl="jdbc:mysql://localhost:3306/student"; private String dbUserName="root"; private String dbPassword="root123"; private String jdbcName="com.mysql.jdbc.Driver"; public Connection getCon() throws Exception{ Class.forName(jdbcName); Connection con=(Connection) DriverManager.getConnection(dbUrl, dbUserName, dbPassword); return con; } public void closeCon(Connection con) throws SQLException{ if(con!=null){ con.close(); } } }