栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用Idea连接Mysql以及基本的连接Mysql方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Idea连接Mysql以及基本的连接Mysql方式

首先,连接Mysql数据库先下载相应的Jar包,首先前往Mysql官网下载

https://dev.mysql.com/downloads/j/选择对应的jar包下载就可以了

就像这样:

然后就是把jar包放在模块的libs包里面就行啦,就像这样

然后点击添加的jar包,右键点击 Add as library准备工作就做完了。

连接数据库的步骤分为四步

        1 注册驱动-加载Driver

        2 获取连接-得到Connection

        3 执行增删改查-发送Sql语句,数据库执行

        4 释放资源

第一种方式,利用DriverManager获取数据库url

public class Test01{
public static void main(String[]args)throws ClassNotFoundException,SQLException{
Connection   con = null;//定义连接对象
Class.forName("com.mysql.cj.jdbc.Driver");//加载数据库驱动类
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_text","root","*****");//通过访问数据库的URL,获取数据库对象,进行连接,第一个为数据库URL,第二个为用户,第三个为本地Mysql数据库登录密码

if(con!=null)
{
System.out.println("第一种方式数据库连接成功");
System.out.println(con);
}
con.close();//关闭连接
    }
}

第二种连接方式,通过反射机制连接数据库,使用反射加载Driver,动态加载更为灵活,减少依赖性

public class Test02{
public static void main(String[]args) throws ClassNotFoundException,InstantiationException,IllegalAccessException,SQLException
{
   Class cl= Class.forName("com.mysql.cj.jdbc.Driver");
    Driver driver = (Driver)cl.newInstance();
    String url = "jdbc:mysql://localhost:3306/jdbc_text";

    Properties info = new Properties();
    info.setProperty("user","root");//写入用户
    info.setProperty("password","*****");//密码

    Connection  connection = driver.connect(url,info);//得到连接

    System.out.println("第二种方式连接数据库n"+connection);
    connection.close();//关闭连接
   }
}

第三种连接方式

public class Test03{
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
      Class cl = Class.forName("com.mysql.cj.jdbc.Driver");//注册驱动
      Driver driver = (Driver) cl.newInstance();
      String url = "jdbc:mysql://localhost:3306/jdbc_text";
           //前面jdbc:mysql://localhost:3306/ //3306是Mysql监听端口号,后面填写你要连接的数据库名字

      String user = "root";//用户名

      String password = "*****";您的Mysql启动密码
      DriverManager.registerDriver(driver);//注册驱动程序
      Connection connection = DriverManager.getConnection(url,user,password);//传入参数
      System.out.println("使用DriverManager连接数据库"+connection);

      connection.close();//关闭连接
      //在编写代码过程中出现异常都可抛出
   }  
}

第三种方式和第二种方式区别不大,大同小异,值得注意的是mysqL驱动5.1.6可以无需CLass . forName(com.mysql.jdbc.Driver"),从jdk1.5以后使用了jdbc4,不再需要显示调用class.forName()注册驱动而是自动调用驱动jar包下meta-INFservicesjava .sql.Driver文本中的类名称自动去注册

第四种连接方式,通过配置文件连接

        

将配置文件的后缀写为properties放入同一个src目录下面

public class Test04 {
   public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
      Properties properties = new Properties();
      properties.load(new FileInputStream("E:\JavaAllFile\IADEJavaFile\JDBC\src\Connection\Connection.properties"));

      String user = properties.getProperty("user");
      String password = properties.getProperty("password");
      String url = properties.getProperty("url");
      String driver = properties.getProperty("driver");

      Class.forName(driver);

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

      System.out.println("第四种通过配置文件连接Mysql数据库:n"+connection);

      connection.close();
   }
}

连接成功! 

总结:

        这四种方式是最基础的数据库连接方式,现在都通过数据库连接池来连接数据库,例如老牌的C3P0,阿里提供的Druid数据库连接池等,感谢你的阅读,如有错误请多多指教。感谢阅读啦,如果能点赞那就更好了。

                                                                         完

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/735756.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号