Java web 之mysql连接

package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class dataSource {
 
 public static Connection getConnection(){
  Connection cn=null;
  try{
   Class.forName("com.mysql.jdbc.Driver").newInstance();
  }
  catch(Exception e){
   System.out.print(e.toString());
  }
  try{
   String url="jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=GBK";
   String user="root";
   String password="123456";
   cn=DriverManager.getConnection(url,user,password);
  }
  catch(SQLException e){
   System.out.print(e.toString());
  }
  return cn;
 }
 public static void main(String[] args) {
  dataSource a= new dataSource();
  Connection b=a.getConnection();
  
  System.out.println(b);

 }
}

 

你可能感兴趣的:(Java web 之mysql连接)