1 package com.dao.db; 2 3 import java.sql.Connection; 4 import java.sql.SQLException; 5 6 /** 7 * 数据库连接层MYSQL 8 * @author Administrator 9 * 10 */ 11 public class DBConnection { 12 13 14 /** 15 * 连接数据库 16 * @return 17 */ 18 public static Connection getDBConnection() 19 { 20 // 1. 注册驱动 21 try { 22 Class.forName("com.mysql.jdbc.Driver"); 23 } catch (ClassNotFoundException e) { 24 // TODO Auto-generated catch block 25 e.printStackTrace(); 26 } 27 // 获取数据库的连接 28 try { 29 Connection conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/mysql?useUnicode=true&characterEncoding=utf-8", "root", "root"); 30 return conn; 31 } catch (SQLException e1) { 32 e1.printStackTrace(); 33 } 34 return null; 35 } 36 37 }
相关文章: