Java连接阿里云RDS-MySQL数据库示例代码

  • 使用的jar包有
    • 在这里插入图片描述
  • URL书写格式
    "jdbc:mysql://外网地址/数据库"

@Test
public void testALiYun() {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		Connection connection = null;
		PreparedStatement prepareStatement = null;
		try {
			dataSource.setDriverClass( "com.mysql.cj.jdbc.Driver" );
			dataSource.setJdbcUrl( "jdbc:mysql://rm-2zek19762f13s8kbo.mysql.rds.aliyuncs.com/bank" );
			dataSource.setUser("root");                                  
			dataSource.setPassword("xxxx");
			
			connection = dataSource.getConnection();
			String sql = "insert into account values(null,'pig',777)";
			prepareStatement = connection.prepareStatement(sql );
			prepareStatement.executeUpdate();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			dataSource.close();
		}
	}
	```
原创文章 116 获赞 207 访问量 66万+

猜你喜欢

转载自blog.csdn.net/sexyluna/article/details/99670786