JDBC 使用和配置

时间:2017-08-23 16:40:04   收藏:0   阅读:314
/**
 * 数据库工具类,封装数据库操作中多次使用的数据库操作代码
 * @author Administrator
 *
 */
public class DBUtils {
	
	public static Connection getConnection() {

		Connection connection = null;
		try {

			Class.forName("com.mysql.jdbc.Driver");

			String databaseName = "jdbc_test";
			String url = "jdbc:mysql://localhost:3306/" + databaseName;
			String user = "root";
			String password = "111";

			connection = DriverManager.getConnection(url, user, password);

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;

	}

}

*Connection connection = DBUtils.getConnection(); 

*PreparedStatement preparedStatement = connection.prepareStatement(sql);

*ResultSet resultSet = preparedStatement.executeQuery();

以上三个流都需要进行关闭  也都可以用try - with - resource 进行关闭  (自动关闭  不需要手动)

技术分享

 

原文:http://www.cnblogs.com/niuxiao12---/p/7418896.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!