现在的位置: 首页 > 综合 > 正文

JAVA 连接MySQL

2013年05月06日 ⁄ 综合 ⁄ 共 1063字 ⁄ 字号 评论关闭
  1. package DB;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. /**
  6.  * 连接MySQL
  7.  * @author zsw
  8.  * 时间:2008年11月27日5:37:09
  9.  */
  10. public class DBconnection {
  11.     private static final String driver="com.mysql.jdbc.Driver";
  12.     private static final String url ="jdbc:mysql://localhost:3306/mydata";
  13.     private static final String name = "root";
  14.     private static final String pwd = "accp";
  15.     
  16.     
  17.     public static Connection getCon(){
  18.         Connection con = null;
  19.         try {
  20.             Class.forName(driver);
  21.             con = DriverManager.getConnection(url,name,pwd);
  22.         } catch (ClassNotFoundException e) {
  23.             System.out.println("驱动加载失败!");
  24.             e.printStackTrace();
  25.         } catch (SQLException e) {
  26.             System.out.println("数据库连接失败!");
  27.             e.printStackTrace();
  28.         }
  29.         
  30.         return con;
  31.     }
  32.     /**
  33.      * @param args
  34.      */
  35.     public static void main(String[] args) {
  36.         Connection con = DBconnection.getCon();
  37.         if(con!=null){
  38.             System.out.println("数据库连接成功!!!!");
  39.         }
  40.     }
  41. }

抱歉!评论已关闭.