JDBC连接mysql数据库

使用JDBC连接数据库

使用jdbc连接数据库的五种方式(建议使用第五种)

   //链接数据库方式一
	@Test
	public void TestConnection1() throws SQLException { 
		
		//获取Driver实现类对象
		Driver driver=new com.mysql.jdbc.Driver(); 
		
		//jdbc:mysql: 协议
		//localhost:ip地址
		//3306 端口号
		//user 数据库名字
		String url = "jdbc:mysql://localhost:3306/user";
		
		//将用户名和密码封装在Properties中
		Properties info = new Properties();
		info.setProperty("user", "root");
		info.setProperty("password","root");
		
		Connection conn=driver.connect(url, info);
		System.out.println(conn);   
	}    
	
	
	
	//方式二对方式一的迭代:在如下的程序中不出现第三方的Api,使得程序具有更好的可移植性
	@Test
	public void TestConnection2() throws Exception {
			//1.获取Driver实现类对象 
		      
		      Class clazz = Class.forName("com.mysql.jdbc.Driver");
		      Driver driver = (Driver) clazz.newInstance(); 
		      
		    //2.提供要链接的数据库
		      String url="jdbc:mysql://localhost:3306/user"; 
		      
		    //提供链接需要的用户名和密码
		      Properties info = new Properties();
				info.setProperty("user", "root");
				info.setProperty("password","root");
				
				Connection conn = driver.connect(url, info);
				System.out.println(conn);	      
	}
	
	
	//方式三:使用DrivaerManager替换Driver
	@Test
	public void TestConnection3() throws Exception {  
		//1.获取Driver实现类对象 
		
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance(); 
		
        //2.提供链接的另外三个基本信息
        String url="jdbc:mysql://localhost:3306/user";
        String user="root";
        String password="root";
        
		//注册驱动
		DriverManager.registerDriver(driver);
		
		//3.获取链接
		Connection conn = DriverManager.getConnection(url, user, password);
		System.out.println(conn);
		
		
	} 
	
	
	//方式四:在三的基础上进行优化
		@Test
		public void TestConnection4() throws Exception {  
			
			 //1.提供链接的另外三个基本信息
	        String url="jdbc:mysql://localhost:3306/user";
	        String user="root";
	        String password="root";
			
			
			//2.获取Driver实现类对象 
			
	         Class.forName("com.mysql.jdbc.Driver");
	       
			Class clazz = null;
			Driver driver = (Driver) clazz.newInstance(); 

			//注册驱动
			DriverManager.registerDriver(driver);
			
			//获取链接
			Connection conn = DriverManager.getConnection(url, user, password);
			System.out.println(conn);
			
		}  



		jdbc.properties:
		user=root
		password=root
		url=jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=UTF-8
driverClass=com.mysql.jdbc.Driver
		
		//方式五:将数据库链接所需要的4个基本信息声明在配置文件中,通过读取配置文件的信息,获取链接 
		@Test
		public void TestConnection5() throws Exception {
			InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
			
			
			 Properties pro = new Properties();
			 pro.load(is); 
			 
			 String user = pro.getProperty("user");
			 String password = pro.getProperty("password");
			 String url = pro.getProperty("url");
			 String driverClass = pro.getProperty("driverClass");
			 
			 //加载驱动
			 Class.forName(driverClass);
			 Connection conn = DriverManager.getConnection(url, user, password);
			 System.out.println(conn);
		}
		

以上为个人学习总结,如果有错误希望大神指点。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值