zoukankan      html  css  js  c++  java
  • JDBC创建链接的几种方式

    首先,使用java程序访问数据库的前提

      数据库的主机地址(ip地址)

      端口

      数据库用户名

      数据库用户密码

      连接的数据库

    代码:

    	private static String url = "jdbc:mysql://localhost:3306/user";
    	
    	private static String username = "root";
    	
    	private static String password = "131411";
    	
    	private static String classDriver = "comm.mysql.jdbc.Driver";
    

     方式一:通过驱动driver类来连接数据库

    		Driver driver = new Driver();					//注册驱动对象
    		Properties prop = new Properties();				//读取信息
    		prop.setProperty("user", username);
    		prop.setProperty("password", password);
    		Connection connect = driver.connect(url, prop);	//获取连接对象
    		System.out.println(connect);
    

     方式二:通过DriverManager,注册Driver,来连接数据库

    		DriverManager.registerDriver(new Driver());
    		Connection connection = DriverManager.getConnection(url, username, password);
    		System.out.println(connection);
    

     方式三:通过反射Driver,加载Driver里面的静态代码块,实现自动注册Driver,在DriverManager连接数据库

    		Class.forName(classDriver);
    		Connection connection = DriverManager.getConnection(url, username, password);
    		System.out.println(connection);
    

     方式四:

    		Driver driver = new Driver();
    		Connection conn = DriverManager.getConnection(url, username, password);
    		System.out.println(conn);
    

     方式五:

    		Connection connection = DriverManager.getConnection(url, username, password);
    		System.out.println(connection);
    

      

  • 相关阅读:
    认识ASP.NET 中的 AppDomain
    试验总结1 改变递归函数中的执行内容
    试验总结2 break与continue
    开篇的话
    01复杂度3 二分查找
    02线性结构2 一元多项式的乘法与加法运算
    01复杂度2 Maximum Subsequence Sum
    02线性结构4 Pop Sequence
    01复杂度1 最大子列和问题
    02线性结构1 两个有序链表序列的合并
  • 原文地址:https://www.cnblogs.com/lldsgj/p/10295495.html
Copyright © 2011-2022 走看看