Jdbc工具类
引言
刚入门学习Jdbc的同学慢慢就会发现,连接数据库的准备工作和收尾动作是一样的内容。
但我们的重心应该放在CURD语句上而不是这些无所谓的工作
所以我们就可以把这些工作封装成工具类,来提高我们的效率。
正文
class JdbcUtils {
Connection connection = null ;
public static final String url = "jdbc:mysql://localhost:3306/db9?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8" ;
public static final String user = "root" ;
public static final String psd = "123456" ;
public static Connection Connect ( ) {
try {
Class . forName ( "com.mysql.cj.jdbc.Driver" ) ;
return DriverManager . getConnection ( url, user, psd) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
} finally {
}
return null ;
}
public static void close ( ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) {
try {
if ( resultSet!= null ) {
resultSet. close ( ) ;
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
try {
if ( preparedStatement!= null ) {
preparedStatement. close ( ) ;
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
try {
if ( connection!= null ) {
connection. close ( ) ;
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
public static String sout ( String resultSet) {
return null ;
}
}
}