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);
    

      

  • 相关阅读:
    散列
    HDOJ 1005
    ASP.NET会话(Session)模式
    【转】SQLServer 2005新功能,一些性能方面问题,sql 经典语句
    使用委托在用户自定义控件中实现事件响应
    Web.config详解 [转]
    每个分类取最新的几条的SQL实现
    c++中const与函数一起用的时候需要注意什么?
    ASP.NET中定制自己的委托和事件参数类
    asp.net repeat嵌套
  • 原文地址:https://www.cnblogs.com/lldsgj/p/10295495.html
Copyright © 2011-2022 走看看