java连接mysql 数据库方式

来源:互联网 发布:跳跃网络 徐智阳 电话 编辑:程序博客网 时间:2024/06/03 09:25


 String driver = "com.mysql.jdbc.Driver";
 String url = "jdbc:mysql://10.27.0.110:3306/health data";//health data为连接的数据库名
 Connection con = null;
 ResultSet res = null;
 String username="xuxiaoying";
 String password="123456";

 public DBhelper() {
  // TODO Auto-generated constructor stub
  
  try {
   // 加载MySql的驱动类
   Class.forName(driver);
  } catch (ClassNotFoundException e) {
   System.out.println("找不到驱动程序类  ,加载驱动失败!");
   e.printStackTrace();
  }

  try {
    con= DriverManager.getConnection(url, username,password);
   System.out.println("数据库连接成功!");
  } catch (SQLException se) {
   System.out.println("数据库连接失败!");
   se.printStackTrace();
  }
 }

0 0