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();
		}
	}
	```

你可能感兴趣的:(数据库,#,Java研发修养)